-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (31 loc) · 800 Bytes
/
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
const http = require("http")
const host = process.env.HOST || "localhost"
const port = process.env.PORT || 3000
function jsonHandler(req, res) {
res.setHeader("Content-Type", "application/json")
res.end(JSON.stringify({ message: "Hello World" }))
}
function defaultHandler(req, res) {
res.setHeader("Content-Type", "text/plain")
res.end("Hello World")
}
function requestHandler(req, res) {
res.statusCode = 200
switch (req.url) {
case "/json": {
jsonHandler(req, res)
break
}
default: {
defaultHandler(req, res)
break
}
}
}
const server = http.createServer(requestHandler)
server.listen(port, host, (err) => {
if (err) {
return console.log("something bad happened", err)
}
console.log(`server is listening on ${host}:${port}`)
})