diff --git a/lib/router.js b/lib/router.js index 2c0ad31..3c920d7 100644 --- a/lib/router.js +++ b/lib/router.js @@ -384,7 +384,8 @@ Router.prototype.routes = Router.prototype.middleware = function () { return next(); } - const path = router.opts.routerPath || ctx.routerPath || ctx.path; + const path = + router.opts.routerPath || ctx.newRouterPath || ctx.path || ctx.routerPath; const matched = router.match(path, ctx.method); let layerChain; diff --git a/package.json b/package.json index bb0fbaa..efff915 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@koa/router", "description": "Router middleware for koa. Maintained by Forward Email and Lad.", - "version": "12.0.0", + "version": "12.1.0", "author": "Alex Mingoia ", "bugs": { "url": "https://github.com/koajs/router/issues", diff --git a/test/lib/router.js b/test/lib/router.js index 67da1e1..065fe91 100644 --- a/test/lib/router.js +++ b/test/lib/router.js @@ -918,7 +918,7 @@ describe('Router', function () { app.use(function (ctx, next) { // bind helloworld.example.com/users => example.com/helloworld/users const appname = ctx.request.hostname.split('.', 1)[0]; - ctx.routerPath = '/' + appname + ctx.path; + ctx.newRouterPath = '/' + appname + ctx.path; return next(); }); app.use(router.routes()); @@ -957,6 +957,42 @@ describe('Router', function () { }); }); + it('two routes with the same path', function (done) { + const app = new Koa(); + const router1 = new Router(); + const router2 = new Router(); + router1.get('/echo/:saying', function (ctx, next) { + try { + expect(ctx.params.saying).eql('helloWorld'); + expect(ctx.request.params.saying).eql('helloWorld'); + next(); + } catch (err) { + ctx.status = 500; + ctx.body = err.message; + } + }); + router2.get('/echo/:saying', function (ctx) { + try { + expect(ctx.params.saying).eql('helloWorld'); + expect(ctx.request.params.saying).eql('helloWorld'); + ctx.body = { echo: ctx.params.saying }; + } catch (err) { + ctx.status = 500; + ctx.body = err.message; + } + }); + app.use(router1.routes()); + app.use(router2.routes()); + request(http.createServer(app.callback())) + .get('/echo/helloWorld') + .expect(200) + .end(function (err, res) { + if (err) return done(err); + expect(res.body).to.eql({ echo: 'helloWorld' }); + done(); + }); + }); + it('parameter added to request in ctx with sub router', function (done) { const app = new Koa(); const router = new Router();