From 000861afacd406f77d64b4a1636c8d3cb5661697 Mon Sep 17 00:00:00 2001 From: JounQin Date: Tue, 18 Sep 2018 21:53:48 +0800 Subject: [PATCH] feat: support array of path with name (sync `all` method) --- lib/router.js | 4 ++-- test/lib/router.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/router.js b/lib/router.js index fe1ef44..941f852 100644 --- a/lib/router.js +++ b/lib/router.js @@ -191,7 +191,7 @@ methods.forEach(function (method) { Router.prototype[method] = function (name, path, middleware) { var middleware; - if (typeof path === 'string' || path instanceof RegExp) { + if (typeof path === 'string' || path instanceof RegExp || Array.isArray(path)) { middleware = Array.prototype.slice.call(arguments, 2); } else { middleware = Array.prototype.slice.call(arguments, 1); @@ -468,7 +468,7 @@ Router.prototype.allowedMethods = function (options) { Router.prototype.all = function (name, path, middleware) { var middleware; - if (typeof path === 'string') { + if (typeof path === 'string' || path instanceof RegExp || Array.isArray(path)) { middleware = Array.prototype.slice.call(arguments, 2); } else { middleware = Array.prototype.slice.call(arguments, 1); diff --git a/test/lib/router.js b/test/lib/router.js index 44e7112..0986c94 100644 --- a/test/lib/router.js +++ b/test/lib/router.js @@ -850,6 +850,16 @@ describe('Router', function () { expect(router.stack[1]).to.have.property('path', '/two'); }); + it('registers array of paths with name', function () { + var router = new Router(); + router.get('name', ['/one', '/two'], function (ctx, next) { + return next(); + }); + expect(router.stack).to.have.property('length', 2); + expect(router.stack[0]).to.have.property('path', '/one'); + expect(router.stack[1]).to.have.property('path', '/two'); + }); + it('resolves non-parameterized routes without attached parameters', function(done) { var app = new Koa(); var router = new Router();