-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateRoute.js
More file actions
54 lines (43 loc) · 1.17 KB
/
createRoute.js
File metadata and controls
54 lines (43 loc) · 1.17 KB
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
var createLayer = require('./createLayer');
var match = require('./utils/match');
var Route = Object.create(Function.prototype);
Route.init = function (method, path) {
this.method = method;
this.path = match(path, undefined);
this.layers = [];
};
Route.addLayer = function (fn) {
this.layers.push(createLayer(fn));
return this;
};
Route.match = function (req) {
if(this.method && req.method !== this.method) return false;
//if(this.path && req.url.split('?')[0] !== this.path) return false;
var ret = this.path(req.url);
if(ret == null) return false;
req.params = ret;
return true;
};
Route.handle = function (err, req, res, next) {
var self = this;
// match test
if(!this.match(req)) return next(err);
// excute layers
return (function loop(i) {
var layer = self.layers[i];
if(!layer) return next(err);
return layer(err, req, res, function (suberr) {
err = suberr;
if(err === 'route') return next();
return loop(i + 1);
});
}(0));
};
module.exports = function createRoute(method, path) {
var route = function (err, req, res, next) {
route.handle(err, req, res, next);
};
route.__proto__ = Route;
route.init(method, path);
return route;
};