-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (54 loc) · 1.71 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const http = require('http')
const https = require('https')
const url = require('url')
const StringDecoder = require('string_decoder').StringDecoder
const fs = require('fs')
const config = require('./config')
const decoder = new StringDecoder('utf-8')
const handlers = {
hello(data, callback) {
callback(200, 'Welcome!')
},
ping(data, callback) {
callback(200)
},
notFound(data, callback) {
callback(404)
},
}
const router = {
hello: handlers.hello,
ping: handlers.ping,
}
const generalServer = (req, res) => {
const parsedUrl = url.parse(req.url, true)
const trimmedPath = parsedUrl.pathname.replace(/^\/+|\/+$/g, '')
let payload = ''
req.on('data', data => payload += decoder.write(data))
req.on('end', () => {
payload += decoder.end()
const handler = router[trimmedPath] || handlers.notFound
const data = {
trimmedPath,
query: parsedUrl.query,
method: req.method.toUpperCase(),
headers: req.headers,
payload,
}
handler(data, (status, payload) => {
const statusCode = Number.isInteger(status) && status || 200
const payloadString = JSON.stringify(payload || {})
res.setHeader('Content-Type', 'application/json')
res.writeHead(status)
res.end(payloadString)
console.log(`response: ${statusCode}, ${payloadString}`)
})
})
}
const httpServer = http.createServer(generalServer)
const httpsServer = https.createServer({
key: fs.readFileSync('./https/key.pem'),
cert: fs.readFileSync('./https/cert.pem'),
}, generalServer)
httpsServer.listen(config.httpsPort, () => console.log(`Listening on port ${config.httpsPort}`))
httpServer.listen(config.httpPort, () => console.log(`Listening on port ${config.httpPort}`))