-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbe.js
More file actions
executable file
·47 lines (42 loc) · 1.08 KB
/
be.js
File metadata and controls
executable file
·47 lines (42 loc) · 1.08 KB
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
#! /usr/bin/env node
const Server = require("./server");
class BackendServer extends Server {
constructor(port) {
super(port);
this.handleRequest();
}
handleRequest() {
this.server.on("request", async (req, res) => {
if (req.url === "/") {
this.generateResponse(
res,
200,
"text/plain",
`Hello From Backend Server ${this.port}\n`
);
console.log(`\nReplied with a hello message`);
} else if (req.url === "/health") {
req.method === "GET"
? this.generateResponse(
res,
200,
"application/json",
JSON.stringify({
statusMessage: "ok",
})
)
: res.writeHead(405).end();
} else {
res.writeHead(404).end(`Page not found\n`);
}
});
}
generateResponse(res, statusCode, contentType, message) {
res
.writeHead(statusCode, {
"Content-Type": contentType,
})
.end(message);
}
}
new BackendServer(process.argv[process.argv.length - 1]);