Skip to content

Commit

Permalink
fix the two routes with the same path conflicts (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanoff authored Apr 2, 2023
1 parent ae98680 commit bc13548
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",

This comment has been minimized.

Copy link
@titanism

titanism Oct 11, 2023

Contributor

Please do not version bump versions in package.json. Leave it up to the maintainers to do so when they publish new versions to npm.

"author": "Alex Mingoia <talk@alexmingoia.com>",
"bugs": {
"url": "https://github.com/koajs/router/issues",
Expand Down
38 changes: 37 additions & 1 deletion test/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit bc13548

Please sign in to comment.