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
When nesting Routers, being able to omit the leading slash appears to be useful, specifically in the situation where the user wants to mount something at exact path the nested Router is mounted on:
constrouter=newRouter();constnestedEntityRouter=newRouter();router.use('/api/entity/:entityId',nestedEntityRouter);// The two following routes can't be mounted on '/', as it'd effectively enforce the strict slashes Router option, even when not desired.nestedEntityRouter.use(-10,async(ctx,next)=>{// Handle querying the resource once, for all routes.// Alternatively: enforce access rules for all routes based on the `entityId`.ctx.state.entity=awaitEntity.findById(ctx.params.entityId);awaitnext();});nestedEntityRouter.get((ctx)=>{ctx.body=ctx.state.entity.serialize();});// Mount nested routers which perform actions on the retrieved entitynestedEntityRouter.get('/process',(ctx)=>{ctx.state.entity.performAction(ctx);});
Obviously, this could be worked around by simply not using a nested router, and instead specifying the full path on all routes.
Arguably, it might also be better for readability to mount the nested Router at '/api/entity', and declare the '/:entityId' parameter on all nested routes. In this instance the existence of entityId was enforced with TypeScript types.
However, another workaround, which I attempted first, is to manually attach the middleware to the nested Router's root node, which turns out to work perfectly fine:
This makes me believe there's no reason not to allow missing leading slashes in the specific instance of mounting at an empty path: .get('', /* ... */).
The text was updated successfully, but these errors were encountered:
When nesting Routers, being able to omit the leading slash appears to be useful, specifically in the situation where the user wants to mount something at exact path the nested Router is mounted on:
Obviously, this could be worked around by simply not using a nested router, and instead specifying the full path on all routes.
Arguably, it might also be better for readability to mount the nested Router at
'/api/entity'
, and declare the'/:entityId'
parameter on all nested routes. In this instance the existence ofentityId
was enforced with TypeScript types.However, another workaround, which I attempted first, is to manually attach the middleware to the nested Router's root node, which turns out to work perfectly fine:
This makes me believe there's no reason not to allow missing leading slashes in the specific instance of mounting at an empty path:
.get('', /* ... */)
.The text was updated successfully, but these errors were encountered: