-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
45 lines (35 loc) · 1.01 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
const { createBareServer } = require("@tomphttp/bare-server-node");
const http = require("http");
const os = require("os");
const bare = createBareServer("/edu/");
const server = http.createServer();
server.on("request", (req, res) => {
bare.routeRequest(req, res);
});
server.on("upgrade", (req, socket, head) => {
bare.routeUpgrade(req, socket, head);
});
let port = parseInt(process.env.PORT || "");
if (isNaN(port)) port = 8080;
server.on("listening", () => {
const address = server.address();
console.log("Listening on:");
console.log(`\thttp://localhost:${address.port}`);
console.log(`\thttp://${os.hostname()}:${address.port}`);
console.log(
`\thttp://${
address.family === "IPv6" ? `[${address.address}]` : address.address
}:${address.port}`
);
});
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
function shutdown() {
console.log("SIGTERM signal received: closing HTTP server");
server.close();
bare.close();
process.exit(0);
}
server.listen({
port,
});