From dde14ae71676a96d612d60ef3b9948f035e5cb9d Mon Sep 17 00:00:00 2001 From: Alex Mingoia Date: Wed, 25 Nov 2015 12:09:33 -0800 Subject: [PATCH] Register multiple routes with array of paths. Closes #203. --- lib/router.js | 10 ++++++++++ test/lib/router.js | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/router.js b/lib/router.js index e3c6c36..862f04e 100644 --- a/lib/router.js +++ b/lib/router.js @@ -490,8 +490,18 @@ Router.prototype.redirect = function (source, destination, code) { Router.prototype.register = function (path, methods, middleware, opts) { opts = opts || {}; + var router = this; var stack = this.stack; + // support array of paths + if (Array.isArray(path)) { + path.forEach(function (p) { + router.register.call(router, p, methods, middleware, opts); + }); + + return this; + } + // create route var route = new Layer(path, methods, middleware, { end: opts.end === false ? opts.end : true, diff --git a/test/lib/router.js b/test/lib/router.js index d1b12af..2c0243e 100644 --- a/test/lib/router.js +++ b/test/lib/router.js @@ -741,6 +741,16 @@ describe('Router', function () { router[method]('/', function () {}).should.equal(router); }); }); + + it('registers array of paths (gh-203)', function () { + var router = new Router(); + router.get(['/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'); + }); }); describe('Router#use()', function (done) {