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.

Colliding async routes don't work as expected

See original GitHub issue

👋 If you have two synchronous routes that overlap as I’d expect:

app.get('/a', c => c.text('a'));
app.get('/:slug', c.text('slug'));

Requesting /a returns a, and requesting /b returns slug.

However, if these routes are asynchronous, then things don’t behave correctly:

app.get('/a', async c => {
    await new Promise(resolve => setTimeout(resolve, 10));
    return c.text('a');
});
app.get('/:slug', async c => {
    await new Promise(resolve => setTimeout(resolve, 10));
    return c.text('slug');
});

Requesting either /a or /b results in slug being returned. Would be great to see this fixed so that there is a deterministic priority for route matching that is consistent.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
MattIPv4commented, May 27, 2022

Just bumped and can confirm this is now behaving in a far more intuitive way to me (first handler that returns stops the chain)! Tyvm!

1reaction
yusukebecommented, May 22, 2022

Hi @MattIPv4

Actually, the behavior in that both handlers are invoked is expected. So,

If I then look in console, I see both slug and a logged

That is not wrong. Because all handlers are treated as Middleware. Every handler are called, and only one of the Response returned from the handlers will be used for the end-user.

It’s in the specifications.

BUT, I notice that this design is not good. In this case, only one handler should be processed, console.log should emit only a.

I’ll consider it. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why Expres Async Errors send different result as expected
According to the document, if I create an error, express-async-errors has to handle this without crashing the application. My question is what I ......
Read more >
Open handle keeps Jest from exiting ( TCPSERVERWRAP)
The server doesn't seem to be closing properly and you will get port collisions when trying to start the server in the different...
Read more >
Using async routes with Express
By composing your Express routes with a simple wrapper, I show how you can take full advantage of async / await very easily....
Read more >
Routing in Next.js – How to Set Up Dynamic ...
In this tutorial, you'll learn how to set up dynamic routing in Next.js. You'll also learn about pre-rendering and why it's important.
Read more >
A Guide to Error Handling in Express.js
Error handling often doesn't get the attention it deserves. Mishandled errors can lead to a bad UX and negatively affect your business.
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