中间件Node Express App中的下一个路由未被触发
我正在使用中间件来调用下一个路由,但由于某种原因,它没有被调用。 这里是代码:
app.get('/foo',function(req,res,next){ console.log('first route') next('route') },function(req,res,next){ // this route is never fired console.log('second route') res.send('second route') })
第二个函数没有被调用。 有任何想法吗
在第一个中间件函数中,你用下面的参数'route'
调用。 正如文档中所述,这会导致后续的callback被绕过:
您可以提供多个callback函数,其行为与中间件类似,只不过这些callback可以调用
next('route')
来绕过剩余的路由callback。
请将您的代码更改为
app.get('/foo',function(req,res,next){ console.log('first route') return next(); },function(req,res,next){ // this route is never fired console.log('second route') res.send('second route') })