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
Hi. I noticed an inconsistent behavior between mounting a router onto an app (app.use(router.routes(), router.allowedMethods())) and onto another router (router.use('/base/path', subRouter.routes(), subRouter.allowedMethods())) when mounting happens before adding routes to the router. This is because when router.use() detects a sub-router it makes a clone of it instead of directly using it as a middleware:
@Alan-Liang, you cann't expect response from empty router (I mean router with out registred routes.)
constKoa=require('koa')constRouter=require('@koa/router')const[r1,r2,r3,r4]=Array(4).fill(0).map(()=>newRouter())// correct behaviorconstapp=newKoa()app.use(r1.routes(),r1.allowedMethods())app.listen(3000)r2.get('/status',ctx=>ctx.body={status: 0})r1.use('/api',r2.routes(),r2.allowedMethods())// badconstbad=newKoa()bad.use(r3.routes(),r4.allowedMethods())bad.listen(3001)// here i register the route for r4r4.get('/status',ctx=>ctx.body={status: 0})// here i use the r4 routes as mwr3.use('/api',r4.routes(),r4.allowedMethods())
perhaps the docs just need to emphasize that routers are cloned at time of binding the middleware, but this feels like an intentional feature - I think it's reasonable to expect that a router would only apply what had already been registered at time of use.
Hi. I noticed an inconsistent behavior between mounting a router onto an app (
app.use(router.routes(), router.allowedMethods())
) and onto another router (router.use('/base/path', subRouter.routes(), subRouter.allowedMethods())
) when mounting happens before adding routes to the router. This is because whenrouter.use()
detects a sub-router it makes a clone of it instead of directly using it as a middleware:router/lib/router.js
Lines 264 to 292 in 344ba0b
I wonder if this is really intentional.
@koa/router
version: 10.0.0Code sample:
And then:
http://localhost:3000/api/status
=> 200{"status":0}
http://localhost:3001/api/status
=> 404Not Found
The text was updated successfully, but these errors were encountered: