-
Notifications
You must be signed in to change notification settings - Fork 34
/
server.js
63 lines (51 loc) · 1.41 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
const mongoose = require('mongoose');
const dotenv = require('dotenv');
process.on('uncaughtException', err => {
console.log('UNCAUGHT EXCEPTION! 💥 Shutting down...');
console.log(err.name, err.message, err);
process.exit(1);
});
// ADDING OUR CONFIG FILE
dotenv.config({ path: './config.env' });
const socket = require('socket.io');
const app = require('./app');
const socketServer = require('./socketServer');
const DB = process.env.DATABASE.replace(
'<PASSWORD>',
process.env.DATABASE_PASSWORD
);
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
})
.then(() => console.log('DB CONNECTION SUCCESSFULL !'));
const port = process.env.PORT || 8080;
const server = app.listen(port, () => {
console.log(`server is running on the port ${port}`);
});
// Mounting our server to socket
// 5e+7 is 50 mb in bytes
// options for the server
const options = {
maxHttpBufferSize: 5e7
};
const io = socket(server, options);
io.on('connection', sock => {
socketServer(sock);
});
process.on('unhandledRejection', err => {
console.log('UNHANDLED REJECTION! 💥 Shutting down...');
console.log(err.name, err.message);
server.close(() => {
process.exit(1);
});
});
process.on('SIGTERM', () => {
console.log('SIGTERM RECIEVED. shutting down gracefully');
server.close(() => {
console.log('process terminated!');
});
});