Middleware shouldn't be async
See original GitHub issueMiddleware created by this module is an async function. That is incorrect because connect framework doesn’t await middleware it runs so any exception within the middleware won’t be caught and propagated properly.
I don’t see a good way to fix it besides dropping support for async to
functions.
I’ve noticed this problem while investigating error that can trigger on res.setHeader
with URL with invalid characters (error TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["Location"]
).
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Writing Async/Await Middleware in Express
I noticed you have: req.render('post', { title, body }); shouldn't it be: res.render('post', { title, body }); ?
Read more >Express middleware cannot trap errors thrown by async/await ...
I thought Express wrapped all middleware functions with try/catch, ... try { await fn(req, res, next); // shouldn't this trap the ...
Read more >The Trouble With Middleware - Async - Django Forum
I have synchronous middleware adapting around asynchronous views just fine, but this does mean we waste a whole synchronous thread per async ......
Read more >Common JS Async / Await Patterns That You Shouldn't Use
Your framework has a middleware to catch errors so it doesn't crash the app and allows you cleaner code. But you weren't aware...
Read more >Why You Shouldn't Mix Promise.then() With Async/Await Syntax
Mixing Promise.then() with async/await syntax is a recipe for bugs. ... traced the issue to an Express middleware function with the following code....
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
Per my last comment, async middleware is not really a problem. Problem is if some of the exceptions are not caught but after #52 all are handled so this is fixed.
Actually, now that I think about it, it’s not really a problem to have an async middleware as long as you are catching errors and passing to next() function.
You can add big try/catch on whole code or something cleaner like an async wrapper function:
and then wrap original middleware function like: