7 let app
= require('../app');
8 const debug
= require('debug')('vc:server');
9 const http
= require('http');
12 * Get port from environment and store in Express.
15 const port
= normalizePort(process
.env
.PORT
|| '3000');
16 app
.set('port', port
);
22 let server
= http
.createServer(app
);
28 let cron
= require('node-cron');
29 const UserModel
= require("../models/User");
30 const GameModel
= require("../models/Game");
31 cron
.schedule('0 0 0 * * *', function() {
32 // Remove unlogged users and unstarted games every 24h
33 UserModel
.cleanUsersDb();
34 GameModel
.cleanGamesDb();
38 * Listen on provided port, on all network interfaces.
42 server
.on('error', onError
);
43 server
.on('listening', onListening
);
49 const WebSocket
= require('ws');
50 const wss
= new WebSocket
.Server({ server: server
});
51 require('../sockets')(wss
);
54 * Normalize a port into a number, string, or false.
57 function normalizePort(val
) {
58 const parsedPort
= parseInt(val
, 10);
60 if (isNaN(parsedPort
)) {
65 if (parsedPort
>= 0) {
74 * Event listener for HTTP server "error" event.
77 function onError(error
) {
78 if (error
.syscall
!== 'listen') {
82 const bind
= typeof port
=== 'string'
86 // handle specific listen errors with friendly messages
89 console
.error(bind
+ ' requires elevated privileges');
93 console
.error(bind
+ ' is already in use');
102 * Event listener for HTTP server "listening" event.
105 function onListening() {
106 const addr
= server
.address();
107 const bind
= typeof addr
=== 'string'
109 : 'port ' + addr
.port
;
110 debug('Listening on ' + bind
);