-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
78 lines (63 loc) · 1.85 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
'use strict';
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
// Login/logout requires
const cors = require('cors');
const cookieParser = require('cookie-parser');
const passport = require('passport');
const session = require('express-session');
const connectMongo = require('connect-mongo');
const database = require('./src/database');
const config = require('./config');
function setupExpress() {
const app = express();
app.use(`/apple-touch-icon`, (req, res) => {
const iconPath = path.join(__dirname, 'assets/favicon/apple-touch-icon.png');
res.sendFile(iconPath, {
maxAge: req.app.enabled('cache') ? 5184000000 : 0,
});
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors({
origin: config.url,
credentials: true,
}));
// for sessions
app.use(session({
name: config.sessionKey,
secret: config.secret,
resave: false,
saveUninitialized: false,
store: connectMongo.create({
mongoUrl: config.dbConnectionString,
}),
cookie: config.cookie,
}));
app.use(cookieParser(config.secret));
app.use(passport.initialize());
app.use(passport.session());
require('./src/passportConfig')(passport);
require('./src/routes')(app);
app.use('/assets', express.static('assets'));
const DIST_DIR = path.join(__dirname, 'assets/dist');
const HTML_FILE = path.join(DIST_DIR, 'index.html');
app.get('*', (req, res) => {
res.sendFile(HTML_FILE);
});
app.use((err, req, res, next) => { // eslint-disable-line no-unused-vars
if (err.code === 'EBADCSRFTOKEN') {
res.status(403).send('csrf-error');
return;
}
res.status(500).json(err.message);
});
app.listen(config.port, () => {
console.log(`listening on port ${config.port}`);
});
}
(async function () {
await database.connect(config.dbConnectionString);
setupExpress();
}());