This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
/
index.js
65 lines (47 loc) · 1.5 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
function Router(routes) {
function router(req, res) {
var i = 0, route, error
while (route = router.routes[i++]) {
req.params = req.url.split("?", 1)[0].match(route.pattern)
if (!req.params) continue
req.params.shift()
route = route.methods[req.method] || route.methods["*"]
if (!route) {
error = new Error("Method Not Allowed")
error.statusCode = 405
router.onerror(error, res)
}
else try {
route.apply(this, arguments)
if (route.length == 1) res.writeHead(204), res.end()
}
catch (error) { router.onerror(error, res) }
return
}
error = new Error("Not Found")
error.statusCode = 404
router.onerror(error, res)
}
router.routes = []
router.route = function(route, methods) {
route = route
.replace(/[-[\]{}()+?.,\\^$|#\s]/g , "\\$&" )
.replace(/:\w+/g , "([^\/]+)")
.replace(/\*\w+/g , "(.*?)" )
if (typeof methods == "function") methods = {"*": methods}
router.routes.push({
pattern: new RegExp("^" + route + "$"),
methods: methods
})
return router
}
router.onerror = function(err, res) {
res.writeHead(err.statusCode || 500, {"Content-Type": "text/plain"})
res.end(err.message || "Internal Server Error")
}
for (var route in routes) router.route(route, routes[route])
return router
}
if (typeof require == "function" && typeof module != "undefined") {
module.exports = Router
}