koa-router
Advanced tools
Comparing version 2.4.3 to 2.4.4
@@ -67,3 +67,2 @@ /** | ||
var captures = []; | ||
params = params || []; | ||
@@ -86,3 +85,5 @@ // save route capture groups | ||
else { | ||
params = captures; | ||
for (var i=0, len=captures.length; i<len; i++) { | ||
params[i] = captures[i]; | ||
} | ||
} | ||
@@ -89,0 +90,0 @@ |
@@ -57,9 +57,7 @@ /** | ||
return function *dispatch(next) { | ||
var routes = []; | ||
var matches = []; | ||
this.params = []; | ||
// Find matching route | ||
debug('%s %s', this.method, this.path); | ||
if (!(routes = router.match(this.method, this.path, this.params))) { | ||
if (!(matches = router.match(this.method, this.path))) { | ||
return yield next; | ||
@@ -69,5 +67,7 @@ } | ||
// Dispatch route middlewares | ||
for (var i=0; i<routes.length; i++) { | ||
debug('dispatch "%s" %s', routes[i].path, routes[i].regexp); | ||
yield routes[i].middleware.call(this, next); | ||
for (var i=0, len=matches.length; i<len; i++) { | ||
var route = matches[i].route; | ||
this.params = matches[i].params; | ||
debug('dispatch "%s" %s', route.path, route.regexp); | ||
yield route.middleware.call(this, next); | ||
} | ||
@@ -193,8 +193,7 @@ }; | ||
* @param {String} path | ||
* @param {Array} params used to store matched params | ||
* @return {Route|false} Returns matched route or false. | ||
* @return {Array|false} Returns matched routes or false. | ||
* @api private | ||
*/ | ||
router.match = function(method, path, params) { | ||
router.match = function(method, path) { | ||
var routes = this.routes[method.toLowerCase()]; | ||
@@ -206,5 +205,10 @@ var matchingRoutes = []; | ||
var params = []; | ||
if (routes[i].match(method, path, params)) { | ||
debug('match "%s" %s', routes[i].path, routes[i].regexp); | ||
matchingRoutes.push(routes[i]); | ||
matchingRoutes.push({ | ||
route: routes[i], | ||
params: params | ||
}); | ||
} | ||
@@ -211,0 +215,0 @@ } |
@@ -9,3 +9,3 @@ { | ||
"author": "Alex Mingoia <talk@alexmingoia.com>", | ||
"version": "2.4.3", | ||
"version": "2.4.4", | ||
"keywords": [ | ||
@@ -12,0 +12,0 @@ "koa", |
@@ -49,2 +49,25 @@ /** | ||
it('populates ctx.params with regexp captures', function(done) { | ||
var app = koa(); | ||
app.use(router(app)); | ||
app.get(/^\/api\/([^\/]+)\/?/i, function *(next) { | ||
this.should.have.property('params'); | ||
this.params.should.be.a('object'); | ||
this.params.should.have.property(0, '1'); | ||
yield next; | ||
}, function *(next) { | ||
this.should.have.property('params'); | ||
this.params.should.be.a('object'); | ||
this.params.should.have.property(0, '1'); | ||
this.status = 204; | ||
}); | ||
request(http.createServer(app.callback())) | ||
.get('/api/1') | ||
.expect(204) | ||
.end(function(err) { | ||
if (err) return done(err); | ||
done(); | ||
}); | ||
}); | ||
it('supports regular expression route paths', function(done) { | ||
@@ -51,0 +74,0 @@ var app = koa(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33016
906