-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
54 lines (44 loc) · 1.35 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
'use strict';
require('dotenv').config();
const express = require('express');
const session = require('express-session');
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo')(session);
const bodyParser = require('body-parser');
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGO_URI, (err) => {
// eslint-disable-next-line
if (err) console.log('mongoose connection error: ' + err);
});
// initialize app
const app = express();
// create io
const http = require('http').Server(app);
const io = require('socket.io')(http);
const apiRouter = require('./src/server/api.js')(io);
app.use('/api', apiRouter);
app.use(bodyParser.urlencoded({extended: false}));
app.use('/public', express.static(__dirname + '/public'));
var sessionOptions = {
secret: process.env.SECRET,
resave: false,
saveUninitialized: true,
store: new MongoStore({
mongooseConnection: mongoose.connection
})
};
if (process.env.ENV_TYPE === 'PRODUCTION') {
sessionOptions.cookie = { secure: true };
app.set('trust proxy', 1);
}
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
app.use((req, res) => {
res.status(404).send('no route was found');
});
const port = process.env.PORT;
http.listen(port || 3000, () => {
// eslint-disable-next-line
console.log('listening on port ' + (port || 3000));
});