Middleware Next Route in Node Express App Not Fired

时间:2017-07-10 15:17:43

标签: node.js express

I am using middleware to invoke the next route but for some reason it is not getting called. Here is the code:

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')

})

The second function is not getting called. Any ideas

2 个答案:

答案 0 :(得分:4)

In the first middleware function, you're calling next with the parameter 'route'. As stated in the documentation, this causes the subsequent callbacks to be bypassed:

You can provide multiple callback functions that behave just like middleware, except that these callbacks can invoke next('route') to bypass the remaining route callback(s).

答案 1 :(得分:0)

请将您的代码更改为

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')

})