-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
80 lines (64 loc) · 1.89 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
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
const express = require('express');
const helmet = require('helmet');
const bodyParser = require('body-parser');
const session = require('express-session');
const path = require('path');
const passport = require('passport');
const fileUpload = require('express-fileupload');
const config = require('config');
const morgan = require('morgan');
const cors = require('cors');
const db = require('./src/db');
const utils = require('./src/utils');
const routes = require('./routes');
const MongoDBStore = require('connect-mongodb-session')(session);
const app = express();
app.use(helmet());
app.use(morgan('combined'));
app.use(cors());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(fileUpload({
limits: {
fileSize: 150000000, // 150mb
},
useTempFiles: false,
tempFileDir: '/tmp/',
abortOnLimit: true, // won't set req.files when too large
limitHandler: (req, res, next) => {
utils.renderError(req, res, 413, 'File size too large! (Max 150MB)');
next();
},
}));
const store = new MongoDBStore({
uri: process.env.DB_URL || config.get('db.url'),
collection: 'sessions',
});
app.set('trust proxy', 1); // trust first proxy
app.use(session({
secret: config.get('app.sessionSecret'),
resave: false,
saveUninitialized: true,
store,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7, // 1 week
},
}));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, '/views'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(passport.initialize());
app.use(passport.session());
app.use('/', routes);
db.connectToServer((error) => {
if (error) {
utils.log('error', 'Could not connect to database');
return;
} else {
utils.log('info', 'Connected to database');
}
app.listen(config.get('app.port'), () => {
utils.log('info', `Server started on port ${config.get('app.port')}`);
});
});
module.exports = app;