-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
60 lines (50 loc) · 1.6 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
const express = require('express');
const http = require('http');
const bodyParser = require('body-parser')
const session = require('express-session');
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo');
const app = express();
const server = http.createServer(app);
const {Server} = require('socket.io');
const io = new Server(server);
require('./modules/socketModule')(io);
require('dotenv').config();
const userRouter = require('./routes/userRouter');
const roomRouter = require('./routes/roomRouter');
const mainRouter = require('./routes/mainRouter');
const gameRouter = require('./routes/gameRouter');
mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
});
const sessionMiddleware = session({
key: 'session',
secret: process.env.SESSION_SECRET,
saveUninitialized: true,
resave: true,
store: MongoStore.create({
mongoUrl: process.env.MONGO_URL,
mongoOptions: {
retryWrites: true
}
})
});
io.use((socket, next) => {
sessionMiddleware(socket.request, socket.request.res, next);
});
app.set('view engine', 'ejs');
app.use(sessionMiddleware);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static('public'));
app.use('/users', userRouter);
app.use('/rooms', roomRouter);
app.use('/game', gameRouter);
app.use('/', mainRouter);
app.use((req, res) => {
return res.status(404).send('Not Found');
});
server.listen(process.env.PORT || '3000');
console.log(`Server started on port ${process.env.PORT || '3000'}`);