-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (48 loc) · 1.78 KB
/
server.js
File metadata and controls
61 lines (48 loc) · 1.78 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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 {ALERT_FROM_EMAIL, ALERT_FROM_NAME, ALERT_TO_EMAIL} = process.env;
const {logger} = require('./utilities/logger');
const {sendEmail} = require('./emailer');
// 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`
function russianRoulette(req, res) {
const errors = [FooError, BarError, BizzError];
const randNum = Math.floor(Math.random() * errors.length);
throw new errors[randNum]('It blew up!');
};
app.use(morgan('common', {stream: logger.stream}));
// for any GET request, we'll run our `russianRoulette` function
app.get('*', russianRoulette);
function doErrorEmailAlerts(err, req, res, next) {
// if it's an error we care about send a message
if (err instanceof FooError || err instanceof BarError) {
logger.info(`Attempting to send error alert email to ${ALERT_TO_EMAIL}`);
const emailData = {
from: ALERT_FROM_EMAIL,
to: ALERT_TO_EMAIL,
subject: `SERVICE ALERT: ${err.name}`,
text: `Something went wrong. Here's what we know:\n\n${err.stack}`
};
sendEmail(emailData);
}
// always want to call next to pass control to next
// middleware function
next();
}
app.use(doErrorEmailAlerts);
// log error and send 500 to 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;
app.listen(port, function () {
logger.info(`Your app is listening on port ${port}`);
});