-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (40 loc) · 1.39 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
const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
const cors = require('cors');
const socketIO = require('socket.io');
require('dotenv').config();
const indexRouter = require('./routes/index');
const sessionRouter = require('./routes/session');
const apiRouter = require('./routes/api');
const db = process.env.DB_URI;
const app = express();
const port = process.env.PORT || 3000;
const server = app.listen(port, (err) => {
console.log(`Server started on ${port}!`);
if (err) throw err;
});
const io = socketIO(server, { cors: true, origins: '*:*' });
mongoose.connect(db, { useUnifiedTopology: true, useNewUrlParser: true })
.then(() => console.log('MongoDB Connected...'))
.catch(err => console.log(err));
mongoose.set('useFindAndModify', false);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors());
app.use('/', indexRouter);
app.use('/session', sessionRouter);
app.use('/api', apiRouter);
io.on('connection', socket => {
console.log('a user connected: ' + socket.id);
socket.on('crash', async(info) => {
console.log(info)
io.sockets.emit('crash', info);
// Disconnect event
})
});
module.exports = app;