Express 文档对 next('route')
回调进行了以下说明:
You can provide multiple callbacks, and all are treated equally, and behave just like middleware, except that these callbacks may invoke next('route') to bypass the remaining route callback(s). You can use this mechanism to perform pre-conditions on a route then pass control to subsequent routes when there is no reason to proceed with the route matched.
和
To skip the rest of the middleware functions from a router middleware stack, call next('route') to pass control to the next route. NOTE: next('route') will work only in middleware functions that were loaded by using the app.METHOD() or router.METHOD() functions.
当中间件调用 next('route')
并且没有其他匹配的路由时,客户端的响应是什么?
最佳答案
对客户端的响应是状态 404 错误。这不会被路由器错误处理程序捕获。
如果您出于某种原因想要捕获 404 错误,请参阅 this问题。
但是要注意匹配的路线:
router.get('/profile', auth.hasRole('User'), controller.showProfile);
router.get('/:id', controller.show);
如果客户端没有该角色,我会调用 hasRole('User') 函数 next('route')
。然后 Controll 被传递到“/:id”路由,因为它与请求 uri 匹配,从而导致错误。
关于express - 当没有其他路由与请求 uri 匹配时调用下一个 ('route' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36535085/