-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
63 lines (53 loc) · 1.65 KB
/
router.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
62
63
// this is our original handler, extracted as a function
const helloWorldHandler = (stream, headers) => {
stream.respond({
':status': 200
})
stream.end('Hello World')
}
// the pingHandler returns "pong" to let us know that
// the server is up and running
const pingHandler = (stream, headers) => {
if (stream.closed) {
return
}
stream.respond({
':status': 200
})
stream.end('pong')
}
// in case a route doesn't exist, we want to return a
// 404 status and a message that the path is not found
const notFoundHandler = (stream, headers) => {
stream.respond({
'content-type': 'text/plain; charset=utf-8',
':status': 200
})
stream.end('path not found')
}
// the router is itself a special type of handler
// where we send the original request to another
// handler based on the pseudo headers
const router = (stream, headers) => {
stream.on('close', () => console.log('stream closed'))
stream.on('error', (err) => console.log('stream error', err))
// first, extract the path and method pseudo headers
const path = headers[':path']
const method = headers[':method']
// we can use a simple if-else ladder to determine the
// final destination for the request stream and assign
// it to the `handler` variable
let handler
if (path === "/hello-world" && method === 'GET') {
handler = helloWorldHandler
}
else if (path === "/ping" && method == 'GET') {
handler = pingHandler
}
else {
handler = notFoundHandler
}
// finally, apply the chosen handler to the request
handler(stream, headers)
}
module.exports = router