-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
67 lines (48 loc) · 1.42 KB
/
index.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
const express = require('express');
const http = require('http');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const SocketIOServer = require('socket.io');
const routes = require('./routes');
const socketio = require('./middlewares/socketio');
const { PORT, MONGODB_URI } = require('./config');
const app = express();
// Create servers
const server = http.createServer(app);
const io = SocketIOServer(server);
// Open MongoDB connection
mongoose.Promise = Promise;
mongoose.connect(MONGODB_URI, {
useMongoClient: true,
});
const db = mongoose.connection;
db.on('error', (error) => {
// eslint-disable-next-line
console.error('Database connection error:', error);
});
db.once('open', () => {
// eslint-disable-next-line
console.log('Database connected!');
});
// Use url body parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Allow CORS
app.use(cors());
// Add socket.io
app.use(socketio(io));
// Use routers
app.use('/v1', routes);
// Start listening
server.listen(PORT, () => {
// eslint-disable-next-line
console.log(`App is listening on ${server.address().port}`);
});
// Handling unhandled rejections
process.on('unhandledRejection', (reason, promise) => {
// eslint-disable-next-line
console.error('Unhandled Rejection at:', promise);
// eslint-disable-next-line
console.error('Reason:', reason);
});