express - 当没有其他路由与请求 uri 匹配时调用下一个 ('route' )

标签 express

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/

相关文章:

html - 如何从 ExpressJS 路由保存渲染的 html View 文件

node.js - 如何让我的 nodejs 应用程序服务于多个用户?

javascript - 错误: StatusCodeError: 413 - {"error": {"code":413 ,"message" :"request entity too large"}}

javascript - 如何在 Node.js 中获取 async.each 的结果

javascript - 当我点击 url 时无法在浏览器中点击路由器

javascript - Mongoose 处理 promise 调用和 Express 中间件中的验证错误

javascript - Apollo 中的 GraphQL 架构存在问题

node.js - Node/Express 开箱即用的问题

json - 错误函数未返回 JSON 格式

node.js - 在expressJS中捕获所有无效的url也是捕获静态或公共(public)文件