-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
90 lines (71 loc) · 1.8 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict'
/**
* restana Web Framework implementation
*
* @license MIT
*/
const requestRouter = require('./libs/request-router')
const exts = {
request: {},
response: require('./libs/response-extensions')
}
module.exports = (options = {}) => {
options.errorHandler = options.errorHandler || ((err, req, res) => {
res.send(err)
})
const server = options.server || require('http').createServer()
const prp = undefined === options.prioRequestsProcessing ? true : options.prioRequestsProcessing
if (prp) {
server.on('request', (req, res) => {
setImmediate(() => service.handle(req, res))
})
} else {
server.on('request', (req, res) => {
service.handle(req, res)
})
}
const handle = (req, res) => {
// request object population
res.send = exts.response.send(options, req, res)
service.getRouter().lookup(req, res)
}
const service = handle
const service_ = {
errorHandler: options.errorHandler,
newRouter () {
return requestRouter(options)
},
getServer () {
return server
},
getConfigOptions () {
return options
},
handle,
start: (...args) => new Promise((resolve, reject) => {
if (!args || !args.length) args = [3000]
server.listen(...args, (err) => {
if (err) reject(err)
resolve(server)
})
}),
close: () => new Promise((resolve, reject) => {
server.close((err) => {
if (err) reject(err)
resolve()
})
})
}
Object.assign(service, service_)
// apply router capabilities
requestRouter(options, service)
service.callback = () => service.handle
service.use(async (req, res, next) => {
try {
await next()
} catch (err) {
return options.errorHandler(err, req, res)
}
})
return service
}