-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
43 lines (32 loc) · 1.24 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';
const express = require('express');
const morgan = require('morgan');
// this will load our .env file if we're
// running locally. On Gomix, .env files
// are automatically loaded.
require('dotenv').config();
const {logger} = require('./utilities/logger');
// these are custom errors we've created
const {FooError, BarError, BizzError} = require('./errors');
const app = express();
// this route handler randomly throws one of `FooError`,
// `BarError`, or `BizzError`
const russianRoulette = (req, res) => {
const errors = [FooError, BarError, BizzError];
throw new errors[
Math.floor(Math.random() * errors.length)]('It blew up!');
};
app.use(morgan('common', {stream: logger.stream}));
// for any GET request, we'll run our `russianRoulette` function
app.get('*', russianRoulette);
// YOUR MIDDLEWARE FUNCTION should be activated here using
// `app.use()`. It needs to come BEFORE the `app.use` call
// below, which sends a 500 and error message to the client
app.use((err, req, res, next) => {
logger.error(err);
res.status(500).json({error: 'Something went wrong'}).end();
});
const port = process.env.PORT || 8080;
const listener = app.listen(port, function () {
logger.info(`Your app is listening on port ${port}`);
});