Multiple routes are executed
See original GitHub issueThere is an open bug in the original koa-router repository which I can still reproduce: https://github.com/ZijianHe/koa-router/issues/231
I think this behaviour makes sense in a way, but is there any solution to set routes to be “exclusive”?
Code sample
const Koa = require('koa');
const Router = require('@koa/router');
const app = new Koa();
const router = new Router();
router.get('/users/all', (ctx, next) => {
console.log('route 1');
ctx.body = 'route 1';
next();
});
router.get('/users/:id(.*)', (ctx, next) => {
console.log('route 2');
ctx.body = 'route 2';
next();
});
app.use((ctx, next) => {
console.log(ctx.request.method, ctx.request.url, 'START');
next();
});
app
.use(router.routes())
.use(router.allowedMethods());
app.use((ctx, next) => {
console.log(ctx.request.method, ctx.request.url, 'END');
next();
});
app.listen(3333);
Expected Behaviour
$ node app
GET /users/all START
route 1
GET /users/all END
Actual Behaviour
$ node app
GET /users/all START
route 1
route 2
GET /users/all END
Versions
@koa/router: 9.1.0
koa: 2.13.0
node: 12.13.1
npm: 6.12.1
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top Results From Across the Web
ExpressJS multiple routes being hit at the same time
I'm setting up my routes for an expressjs app, and I'm seeing 2 routes being executed when I hit one endpoint. Here is...
Read more >How to create multiple routes in the same express.js server
Express. js allows us to create multiple routes on a single express server. ... Run index.js using the below command: node index.js.
Read more >Bug: 2 overlapping routes on the same level are executed #231
The router iterates through the routing table on each incoming request and executes the first (and only the first) matching route. Route matching...
Read more >Let's Learn AdonisJS 5: Multi-File Route Grouping Strategies
In this lesson, we'll discuss strategies you can use to apply multiple files worth of route definitions into a single route group without...
Read more >Choice Router | MuleSoft Documentation
The Choice router dynamically routes messages through a flow according to a set of DataWeave expressions that evaluate message content.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Old issue but a solution is to enforce the requirement for the second route to be a number.
Changing the match path to
'/users/:id(\\d+)'
will ensure that it will only match if the:id
is of type decimal.Hi @dios-david, were you able to find a workaround for this? I am facing the same issue. Don’t know if it’s a feature or a bug at this point.