-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (57 loc) · 1.47 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
/*
* Celer - An HTTP framework
* Copyright(c) 2017-present @GavinDmello
* MIT Licensed
*/
const http = require('http')
class Celer {
constructor(opts) {
let Router = undefined
let that = this
opts = opts || {}
if (!(this instanceof Celer)) {
return new Celer()
}
switch (opts.router) {
case 'event':
Router = require('./lib/event-router')
break
case 'object':
Router = require('./lib/object-router')
break
default:
Router = require('./lib/object-router')
}
this.router = new Router()
this.server = http.createServer((req, res) => {
that.dispatcher(req, res)
})
}
dispatcher(req, res) {
this.router.send(req, res)
}
get(url, handler) {
this.router.when(url, 'get', handler)
}
post(url, handler) {
this.router.when(url, 'post', handler)
}
put(url, handler) {
this.router.when(url, 'put', handler)
}
head(url, handler) {
this.router.when(url, 'head', handler)
}
patch(url, handler) {
this.router.when(url, 'patch', handler)
}
delete(url, handler) {
this.router.when(url, 'delete', handler)
}
listen(port, callback) {
callback = callback || noop
this.server.listen(port, callback)
}
}
function noop() {}
module.exports = Celer