-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
48 lines (42 loc) · 1.21 KB
/
server.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
const http = require('http')
const dispatch = (url, cb) => {
const [path, query] = url.split('?')
const [_, moduleName, methodName] = path.split('/')
const module = require(`./lib/${moduleName}`)
let paramsObj = {}
if (query) {
const params = query.split('&')
for (let i = 0; i < params.length; i++) {
const items = params[i].split('=')
paramsObj[items[0]] = items[1]
}
}
const methodCaller = module[`${methodName}`](paramsObj)
cb(methodCaller)
}
const httpHandler = (request, response) => {
// Requests
// const method = request.method
const url = request.url
// Response
response.setHeader('Content-Type', 'application/json')
response.setHeader('X-Powered-By', 'Stomatic Clot')
if (url !== '/favicon.ico') {
const startTime = new Date()
dispatch(url, (content) => {
const newResponse = response
newResponse.write(content)
newResponse.end()
const endTime = new Date() - startTime
console.info(`[%s] %s %dms\n`, request.method, url, endTime)
})
}
}
const server = http.createServer(httpHandler)
const listenCallbackFn = (err) => {
if (err) {
throw Error
}
console.log('Server is running')
}
server.listen(6543, listenCallbackFn)