退出当前的中间件链而不抛出错误

Exiting the current chain of middleware without throwing error

我正在寻找一种无需调用 next() 即可退出中间件链的方法。

下面是一个例子。这有效,但我必须每次检查 req.filter 以确保将其设置为 false。 express 是否有任何方法可以在不抛出错误的情况下退出当前的中间件链(移动到下一条路线)(next(new Error("will exit and move to error handler"))?

我真正想要的是一个 nextRoute() 处理程序,它将退出此路由的中间件堆栈并移动到下一个。

function queryExists(query){
  return function(req, res, next){
    if(!dotty.exists(req, "query."+query)) req.filter = true
    return next()
  }
}

app.get("/", queryExists("foo"), queryExists("bar"), function(req, res, next){
  if(req.filter) return next()
  return res.send(req.query.foo + " | " + req.query.bar)
})

app.get("/", function(req, res, next){
  return res.send("hi")
})

我喜欢以任何标准方式解决此问题的框架或库。

If you need to skip the rest of the middleware from a router middleware stack, call next('route') to pass on the control to the next route. Note: next('route') will work only in middleware loaded using app.VERB() or router.VERB().

return next('route')