-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroute.js
43 lines (36 loc) · 837 Bytes
/
route.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
/* eslint prefer-reflect: ["error", { "exceptions": ["call"] }]*/
/* eslint-disable func-style, vars-on-top*/
function wrapHandler(path, cb) {
return function (req, res, next) {
if (req.url === path) {
return cb(req, res);
}
return next();
};
}
function notFoundHandler(req, res) {
res.writeHead(404, {'Content-Type': 'text/html'});
res.write('No Path found');
res.end();
}
function compose(mw) {
return function (req, res) {
next = function () {
notFoundHandler.call(this, req, res);
};
i = mw.length;
while (i) {
i -= 1;
thisMiddleware = mw[i];
nextMiddleware = next;
next = function () {
thisMiddleware.call(this, req, res, nextMiddleware);
};
}
return next();
};
}
module.exports = {
wrap: wrapHandler,
comp: compose
};