-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (56 loc) · 1.51 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
const express = require('express');
const http = require('http');
const bodyParser = require('body-parser');
const session = require('express-session');
const MongoStore = require('connect-to-mongo')(session);
const cookieParser = require('cookie-parser');
const serveStatic = require('serve-static');
const logger = require('./utils/logger');
const config = require('./config')();
const _ = require('underscore');
async function init() {
const app = express();
const server = http.Server(app);
require('./utils/express/async')(app);
await require('./db').init();
app.set('views', './views');
app.set('view engine', 'pug');
app.locals = require('./views/helpers');
app.use(bodyParser.json());
app.use(cookieParser('1ea87c9bd1ef415eaf11599cce75836f', {
path: '/',
httpOnly: true,
maxAge: null
}));
app.use(session({
secret: config.authSecret,
store: new MongoStore({
collection: 'authSessions',
...config.mongodb
}),
resave: false,
saveUninitialized: false
}));
app.use('/static', serveStatic('./static'));
require('./routes')(app);
server.listen(config.listen);
require('./socket')(server);
logger(`Server started on ${config.listen.host}:${config.listen.port}`);
app.use((err, req, res, next) => {
logger(err);
if (res.headersSent) {
return next(err);
}
res.status(err.statusCode || 500).json(
_(err).pick('name', 'message', 'userMessage')
);
});
}
if (module.parent) {
module.exports = init;
} else {
init().catch((err) => {
logger(err);
process.exit(1);
});
}