-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (45 loc) · 1.48 KB
/
index.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
require("dotenv").config();
const http = require("http");
const debug = require("debug")("genux-service:server");
//import the app module
const app = require("./app");
//set the port for the server
const port = parseInt(process.env.PORT || "3000", 10);
//set port for the app to listen on
app.set("port", port);
//create server with set timeout limit
const server = http.createServer(app);
const timeout = process.env.TIME_OUT * 60 * 1000;
server.setTimeout(timeout);
//function to handle errors related to the server connection
const onError = (error) => {
if (error.syscall !== "listen") throw error;
//determine which specific error occurred and log message accordingly
const bind = "Port " + port;
switch (error.code) {
case "EACCESS":
console.error(bind + " requires elevated privileges");
process.exit(1);
case "EADDRINUSE":
console.error(bind + "is already in use");
process.exit(1);
default:
throw error;
}
};
const onListening = () => {
const address = server.address();
//log the port that the server is listening on
debug("Listening on Port: ", port);
};
//listen on the specified port
server.listen(port, () => {
const address = server.address();
//log a message when the server has been successfully started
console.log("Server is running on port ", port);
});
//listen for any errors or successful connections
server.on("error", onError);
server.on("listening", onListening);
//export the app module
module.exports = app;