reorder scripts inclusion (should use Webpack instead...)
[qomet.git] / bin / www
CommitLineData
e99c53fb
BA
1#!/usr/bin/env node
2
3/**
4 * Module dependencies.
5 */
6
29c8b391
BA
7const app = require('../app');
8const http = require('http');
e99c53fb
BA
9
10/**
11 * Get port from environment and store in Express.
12 */
13
29c8b391 14const port = normalizePort(process.env.PORT || '3000');
e99c53fb
BA
15app.set('port', port);
16
17/**
18 * Create HTTP server.
19 */
20
29c8b391 21const server = http.createServer(app);
e99c53fb
BA
22
23/*
24 * CRON tasks
25 */
26
29c8b391
BA
27const cron = require('node-cron');
28const UserModel = require("../models/user");
e99c53fb
BA
29cron.schedule('0 0 0 * * *', function() {
30 // Remove unlogged users every 24h
31 UserModel.cleanUsersDb();
32});
33
34/**
35 * Listen on provided port, on all network interfaces.
36 */
37
38server.listen(port);
39server.on('error', onError);
40server.on('listening', onListening);
29c8b391 41const io = require('socket.io').listen(server);
e99c53fb
BA
42
43/*
44 * Sockets handling
45 */
46
47require('../sockets')(io);
48
49/**
50 * Normalize a port into a number, string, or false.
51 */
52
53function normalizePort(val)
54{
29c8b391 55 const port = parseInt(val, 10);
e99c53fb
BA
56
57 if (isNaN(port)) // named pipe
58 return val;
59
60 if (port >= 0) // port number
61 return port;
62
63 return false;
64}
65
66/**
67 * Event listener for HTTP server "error" event.
68 */
69
70function onError(error)
71{
72 if (error.syscall !== 'listen')
73 throw error;
74
75 let bind = typeof port === 'string'
76 ? 'Pipe ' + port
77 : 'Port ' + port;
78
79 // handle specific listen errors with friendly messages
80 switch (error.code)
81 {
82 case 'EACCES':
83 console.error(bind + ' requires elevated privileges');
84 process.exit(1);
85 break;
86 case 'EADDRINUSE':
87 console.error(bind + ' is already in use');
88 process.exit(1);
89 break;
90 default:
91 throw error;
92 }
93}
94
95/**
96 * Event listener for HTTP server "listening" event.
97 */
98
99function onListening()
100{
29c8b391
BA
101 const addr = server.address();
102 const bind = typeof addr === 'string'
e99c53fb
BA
103 ? 'pipe ' + addr
104 : 'port ' + addr.port;
105 console.log('Listening on ' + bind);
106}