7 var app
= require('../app');
8 var debug
= require('debug')('vc:server');
9 var http
= require('http');
12 * Get port from environment and store in Express.
15 var port
= normalizePort(process
.env
.PORT
|| '3000');
16 app
.set('port', port
);
22 var server
= http
.createServer(app
);
28 var cron
= require('node-cron');
29 var UserModel
= require("../models/User");
30 var ChallengeModel
= require("../models/Challenge");
31 var GameModel
= require("../models/Game");
32 cron
.schedule('0 0 0 * * *', function() {
33 // Remove some old users, challenges and games every 24h
34 UserModel
.cleanUsersDb();
35 ChallengeModel
.removeOld();
36 GameModel
.cleanGamesDb();
40 * Listen on provided port, on all network interfaces.
44 server
.on('error', onError
);
45 server
.on('listening', onListening
);
51 const WebSocket
= require('ws');
52 const wss
= new WebSocket
.Server({ server: server
});
53 require('../sockets')(wss
);
56 * Normalize a port into a number, string, or false.
59 function normalizePort(val
) {
60 var port
= parseInt(val
, 10);
76 * Event listener for HTTP server "error" event.
79 function onError(error
) {
80 if (error
.syscall
!== 'listen') {
84 var bind
= typeof port
=== 'string'
88 // handle specific listen errors with friendly messages
91 console
.error(bind
+ ' requires elevated privileges');
95 console
.error(bind
+ ' is already in use');
104 * Event listener for HTTP server "listening" event.
107 function onListening() {
108 var addr
= server
.address();
109 var bind
= typeof addr
=== 'string'
111 : 'port ' + addr
.port
;
112 debug('Listening on ' + bind
);