-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
71 lines (60 loc) · 1.7 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
64
65
66
67
68
69
70
71
'use strict';
/**
* Module dependencies.
*/
import chalk from 'chalk';
import http from 'http';
import logger from './server/logging/logger';
const port = 4242;
export const start = () => {
// Init the express application
const app = require('./server/index')();
// Get port from environment and store in Express.
app.set('port', port);
// Create HTTP server.
const server = http.createServer(app);
server.listen(port);
server.on('error', onError);
server.on('listening', logStartMessages);
// Error Message Handlers
function onError(error) {
// log error
logger.error(new Error(error), error.message);
// handle specific listen errors with friendly messages
if (error.errno === 'EADDRINUSE') {
console.error(
'(EADDRINUSE) Cannot start ' + "server" + '.',
'Port ' + port + ' is already in use by another program.'
);
} else {
console.error(
'(Code: ' + error.errno + ')',
'There was an error starting your server.',
'Please use the error code above to search for a solution.'
);
}
process.exit(-1);
}
// Startup & Shutdown messages
function logStartMessages() {
console.log(
chalk.green("The server" + ' has been started...'),
'\nYour application is now available on',
'http://localhost' + ':' + port,
chalk.gray('\nCtrl+C to shut down')
);
function shutdown() {
console.log(chalk.red('\n' + "Server" + ' has shut down'));
console.log(
'\n' + "Server" + ' was running for',
Math.round(process.uptime()),
'seconds'
);
process.exit(0);
}
// ensure service exits correctly on Ctrl+C and SIGTERM
process.
removeAllListeners('SIGINT').on('SIGINT', shutdown).
removeAllListeners('SIGTERM').on('SIGTERM', shutdown);
}
};