question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple routes are executed

See original GitHub issue

There 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
dsmackiecommented, Aug 26, 2020

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.

1reaction
sichaturcommented, Jul 21, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found