-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
75 lines (60 loc) · 1.63 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
72
73
74
/*eslint-env node*/
require('babel-core/register')();
var chalk = require('chalk');
var config = require('./server/config');
var debug = require('debug')('app:server');
var fs = require('fs');
var util = require('util');
// Write PID to file
fs.writeFile('server.pid', process.pid, (err) => {
if (err)
console.error('Error writing PID file!')
});
/**
* App Setup
*/
var app = module.exports = require('./server/app.js')();
app.ready
.then(() => {
if (config.interface)
app.listen(config.port, config.interface, onListen);
else
app.listen(config.port, onListen);
});
function onListen() {
debug(`${chalk.green(config.app.title)} listening on port ${chalk.green(config.port)} in ${chalk.green(process.env.NODE_ENV || 'development')} mode`);
}
function shutdown() {
debug("Received kill signal, shutting down gracefully.");
app.close(function () {
debug("Closed out remaining connections.");
process.exit()
});
// if after
setTimeout(function () {
debug("Could not close connections in time, forcefully shutting down");
process.exit()
}, 10 * 1000);
}
process.on('uncaughtException', function(err) {
console.error(chalk.red('Caught unhandled exception!'));
console.error(err);
throw err;
});
/**
* Signal handling
*/
process.on('SIGINT', () => {
debug('Got SIGINT');
process.exit(0);
});
// SIGUSR2 is used by nodemon to restart the app, only bind in production
if (process.env.NODE_ENV == 'production')
process.on('SIGUSR2', () => {
debug('Got SIGUSR2, shutting down gracefully!');
shutdown();
});
process.on('SIGPIPE', () => {
debug('Got SIGPIPE');
console.log(util.inspect(process.memoryUsage()));
});