forked from ankitkumar1578114/co-back
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
61 lines (51 loc) · 1.55 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
#!/usr/bin/env node
/**
* @file_brief -> This file specifies error handlers, and mounts the express app on a node.js server (exported from ${REPO_ROOT}/app.js)
*/
const app = require("./app");
const { exit } = require("process");
const PORT = process.env.PORT || "8080";
app.set("port", PORT);
/**
* @brief - Event listener for HTTP server "error" event.
*
* @param - `error` is the error object
* @returns process.exit call
*/
function onError (error) {
if (error.syscall !== "listen") {
throw error;
}
const bind = typeof PORT === "string"
? "Pipe " + PORT
: "Port " + PORT;
// handle specific listen errors with friendly messages
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges");
return process.exit(1);
case "EADDRINUSE":
console.error(bind + " is already in use");
return process.exit(1);
default:
throw error;
}
}
function onListening () {
// const addr = server.address();
// const bind = typeof addr === "string"
// ? "pipe " + addr
// : "port " + addr.port;
console.log("Listening on " + PORT);
}
function exitHandler(sig) {
console.log(`Recieved ${sig}: Exiting gracefully`);
server.close((err) => {
console.log("Couldn't close server, due to ",err);
});
exit(0);
}
process.on("SIGTERM", exitHandler);
process.on("SIGINT", exitHandler);
app.on("error", onError);
const server = app.listen(PORT, onListening);