You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just started using koa@2.x together with koa-router@7.x and noticed that middlewares don't get executed if no defined route matches, coming from express this was kinda unexepected. I searched if this was by design because even examples in the README don't seem to expect this kind of behavior, e.g:
I found the following issue: #182
which was fixed by: e486a93
The comment made just some days ago by @rockie suggest that i'm not alone with this issue and that this problem also exists in the 5.x version of koa-router.
If I remove the route check for my project, everything works as expected.
Take the following code as example, I want the API to respond with JSON for all errors allowedMethods throws on the /api route and to respond with HTML for everything else:
I expect POST /api/entity to return a JSON response:
{
"error": "Method Not Allowed"
}
but the middleware will be ignored completely and the top level allowedMethods will handle it and return HTML:
Method Not Allowed
This example also works as expected if the route check is removed.
I can think of many more use cases where middleware without matching routes are required. So is this behaviour really by design or am I doing something completely wrong?
The text was updated successfully, but these errors were encountered:
The whole point of route-specific middleware is to run middleware for a specific route. Otherwise there would be no difference between app.use() and router.use(). If you want middleware to run regardless of any route matching you can mount it on the app via app.use().
Instead of nesting the apiRouter as route middleware for router, mount apiRouter separately using app.use('/api', apiRouter.routes(), apiRouter.allowedMethods())
@aheckmann Yeah, you're right. I gave it some more thought after I wrote this issue and it's actually more logical that way and the example above is actually the only use case where it would profit from working the other way, but there are some other clean solutions for that problem.
I'll close this issue. Thanks for the quick response tho, nice project you got here 👍
I just started using
koa@2.x
together withkoa-router@7.x
and noticed that middlewares don't get executed if no defined route matches, coming from express this was kinda unexepected. I searched if this was by design because even examples in the README don't seem to expect this kind of behavior, e.g:https://github.com/alexmingoia/koa-router#nested-routers (posts.allowedMethods() middleware will never be called, because no route matches the other HTTP methods)
I found the following issue: #182
which was fixed by: e486a93
The comment made just some days ago by @rockie suggest that i'm not alone with this issue and that this problem also exists in the 5.x version of koa-router.
If I remove the route check for my project, everything works as expected.
Take the following code as example, I want the API to respond with JSON for all errors
allowedMethods
throws on the/api
route and to respond with HTML for everything else:I expect
POST /api/entity
to return a JSON response:but the middleware will be ignored completely and the top level
allowedMethods
will handle it and return HTML:This example also works as expected if the route check is removed.
I can think of many more use cases where middleware without matching routes are required. So is this behaviour really by design or am I doing something completely wrong?
The text was updated successfully, but these errors were encountered: