-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
41 lines (32 loc) · 934 Bytes
/
routes.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
const Winston = require('winston')
module.exports = (dispatcher) => {
Winston.info('routes registered')
dispatcher.setStatic('/resources')
dispatcher.setStaticDirname('static')
// html pages
dispatcher.onGet('/', (req, res) => {
res.writeHead(200, {'Content-Type': 'text/html'})
res.end('<h1>Index Page</h1>')
})
// apis
dispatcher.onGet('/api/customer', (req, res) => {
const customer = {
firstName: 'Tony',
lastName: 'Stark'
}
res.writeHead(200, {'Content-Type': 'application/json'})
res.end(JSON.stringify(customer))
})
dispatcher.onPost('/api/customer', (req, res) => {
Winston.info(req.body)
const response = {
status: 'success'
}
res.writeHead(201, {'Content-Type': 'application/json'})
res.end(JSON.stringify(response))
})
dispatcher.onError = (req, res) => {
res.writeHead(404)
res.end('<h1>Resource not found</h1>')
}
}