-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
168 lines (147 loc) · 4.68 KB
/
app.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
require('dotenv').config({ path: './keys.private.env' }); // Setup environment
const express = require('express');
const HttpStatus = require('http-status-codes');
const path = require('path');
const passport = require('passport');
const session = require('express-session');
const cors = require('cors');
const auth = require('./lib/auth');
const config = require('./lib/utils/config');
const { httpLogger, logger } = require('./lib/utils/loggers');
const mongoSetup = require('./lib/utils/mongo');
const router = require('./lib/routes');
const app = express();
app.set('env', config.env);
// Templating engine
app.set('view engine', 'pug');
// Logging - to stdout in development, to files in production
app.use(httpLogger);
app.use(cors());
// Static directory
// The dist directory is where webpack builds to
app.use(express.static(path.join(__dirname, 'dist')));
// Passport + session setup
if (app.get('env') === 'production') {
const MongoStore = require('connect-mongo')(session);
// Session setup
app.use(session({
secret: config.session.secret,
store: new MongoStore(mongoSetup.storeOptions),
saveUninitialized: true,
resave: false
}));
} else {
// Session setup
// Dev store doesn't need persistence
app.use(session({
secret: config.session.secret,
saveUninitialized: true,
resave: false
}));
}
const { strategy, deserializeUser, serializeUser } = auth;
passport.use(strategy);
passport.serializeUser(serializeUser);
passport.deserializeUser(deserializeUser);
app.use(passport.initialize());
app.use(passport.session());
// Route setup
app.use(router);
if (app.get('env') === 'production') {
// @see https://expressjs.com/en/advanced/best-practice-performance.html#in-code
// Compress everything
const compression = require('compression');
app.use(compression());
// Error handling: Send less explicit errors in production
app.use((err, req, res, next) => {
const status = err.status || HttpStatus.INTERNAL_SERVER_ERROR;
const explicitDescription = err.description || err.message || 'Unknown Error';
let description;
switch (status) {
case HttpStatus.NOT_FOUND:
description = 'Not found!';
break;
case HttpStatus.BAD_REQUEST:
description = 'Bad Request!';
break;
case HttpStatus.FORBIDDEN:
description = 'Not authenticated!';
break;
default:
description = 'Server Error :/';
}
logger.error(`ERROR CODE ${status}: ${explicitDescription}`);
// Pass less descriptive error along in the locals
res.locals.errorCode = status;
res.locals.errorDescription = description;
next();
});
} else {
// Development
// Watches for file changes
const webpack = require('webpack'); // eslint-disable-line
const wpConfig = require('./webpack.config');
wpConfig.watch = true;
wpConfig.watchOptions = {
aggregateTimeout: 500,
poll: 500
};
webpack(wpConfig, (err, stats) => {
if (err) {
logger.error(`Webpack build error: ${err.stack || err}`);
} else {
logger.info('Rebuilt bundles.');
const statsInfo = stats.toJson();
if (stats.hasErrors()) {
logger.error(`Webpack build errors: ${statsInfo.errors}`);
}
if (stats.hasWarnings()) {
logger.error(`Webpack build warnings: ${statsInfo.warnings}`);
}
logger.info(`Build took: ${statsInfo.time}ms`);
}
});
// Error handling: Send more explicit errors
app.use((err, req, res, next) => {
const description = err.description || err.message || 'Unknown Error';
const status = err.status || HttpStatus.INTERNAL_SERVER_ERROR;
logger.error(`ERROR CODE ${status}: ${description}`);
// Pass error along
res.locals.errorCode = status;
res.locals.errorDescription = description;
next();
});
}
app.use((req, res) => {
// if coming from the above error handler, error code will be set in locals
// Otherwise it's a not found error
const errorCode = res.locals.errorCode || HttpStatus.NOT_FOUND;
const errorDescription = res.locals.errorDescription || 'Not Found!';
res.status(errorCode);
// respond with html page
if (req.accepts('html')) {
res.render('error', { title: 'Error', errorCode, errorDescription });
return;
}
// respond with json
if (req.accepts('json')) {
res.json({ errorDescription });
return;
}
// default to plain-text. send()
res.type('txt').send(errorDescription);
});
function start() {
// Databse setup
mongoSetup();
app.listen(config.port, () => {
logger.info(`${config.name} is listening on port ${config.port}`);
});
}
// If run directly
if (require.main === module) {
start();
}
// Exported for testing
module.exports = app;
module.exports.start = start;