forked from kriasoft/graphql-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
42 lines (36 loc) · 1.3 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
/**
* Node.js API Starter Kit (https://reactstarter.com/nodejs)
*
* Copyright © 2016-present Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
/* @flow */
/* eslint-disable no-console, no-shadow */
import app from './app';
import db from './db';
import redis from './redis';
const port = process.env.PORT || 8080;
const host = process.env.HOSTNAME || '0.0.0.0';
// Launch Node.js server
const server = app.listen(port, host, () => {
console.log(`Node.js API server is listening on http://${host}:${port}/`);
});
// Shutdown Node.js app gracefully
function handleExit(options, err) {
if (options.cleanup) {
const actions = [server.close, db.destroy, redis.quit];
actions.forEach((close, i) => {
try {
close(() => { if (i === actions.length - 1) process.exit(); });
} catch (err) { if (i === actions.length - 1) process.exit(); }
});
}
if (err) console.log(err.stack);
if (options.exit) process.exit();
}
process.on('exit', handleExit.bind(null, { cleanup: true }));
process.on('SIGINT', handleExit.bind(null, { exit: true }));
process.on('SIGTERM', handleExit.bind(null, { exit: true }));
process.on('uncaughtException', handleExit.bind(null, { exit: true }));