12

我有一个用于这条路由的“路由特定中间件”链,如下所示:

    var express = require('express');
    var server = express();
    var mw1 = function(req, resp, next) {
        //do stuff
        if (success) {
            next();
        } else {
            req.connection.destroy(); //without calling next()
        }
    };
    var mw2 = function(req, resp, next) {
        //do stuff
        if (success) {
            next();
        } else {
            req.connection.destroy(); //without calling next()
        }
    };
    server.post('/some/path', [mw1, mw2], function(req, resp) {
        //write response
    });

[mw1, mw2]是特定于路由的中间件/some/path

这与服务器范围的中间件不同,如下所示:

    server.use(mw1);
    server.use(mw2);

它适用于所有定义的路由。

现在我的问题是我想退出链条。即如果success是假的mw1,我不希望mw2被调用。如果success是 false in mw2,我不会不调用路由函数。目前,无论是否被调用,两者似乎都被mw1调用了——我不知道为什么。mw2next()

我该怎么做呢?

4

3 回答 3

20

您可以调用next( 'route' ),如express api 参考中所述,application routing部分

可以给出多个回调,所有回调都被平等对待,并且表现得就像中间件一样,除了一个例外,这些回调可能会调用next('route')以绕过剩余的路由回调。

例子

var express = require('express')
  , app = express()
;

// keep an eye on the function names
app.post( '/some/path', middleware1, middleware2, function route1( req, res, next ) {
      // write response
});
app.all( '*', function route2( req, res, next ) {
    // do something like handle a 404 request
});
app.use(function errorHandler( err, req, res, next ) {
    // handle error
});

function middleware1( req, res, next ) {
    // ...
    if ( !success ) {
        // bypasses middleware2 and route1, route2 will be called
        return next( 'route' );
    }
    // calls middleware2
    next();
}
// intentionally similar to middleware1 to keep things clear
function middleware2( req, res, next ) {
    if ( !success ) {
      // bypasses route1 and route2
      // errorHandler will be called with the error
      return next( Error( 'middleware 2 failed' ) );
    }
    // calls route1
    next();
}
于 2013-06-21T09:56:25.620 回答
2

稍加修改就得出了答案:

var express = require('express');
var server = express();
var mw1 = function(req, resp, next) {
  //do stuff
  if (success) {
    next();
  } else {
    resp.send(406, 'Invalid because of this');
    req.connection.destroy(); //without calling next()
  }
};
var mw2 = function(req, resp, next) {
  //do stuff
  if (success) {
    next();
  } else {
    resp.send(406, 'Invalid because of that');
    req.connection.destroy(); //without calling next()
  }
};
server.post('/some/path', [mw1, mw2], function(req, resp) {
  //write response
});

诀窍是发送响应:resp.send(406, 'Invalid because of this');

就在破坏连接之前:req.connection.destroy();

实际上并没有破坏连接,我发现在一般情况下也可以工作。

(但在我的具体情况下是必需的,并且超出了这个问题的范围。)

如果已经发送了响应,则 express 不会自动呼叫next()您,因为它似乎并非如此。

于 2013-06-21T16:11:11.093 回答
1

我的印象是,如果您next()在路由处理函数中既不调用也不发送响应,express 就会挂起。FWIW我还没有使用数组,我的看起来像server.post('/some/path', mw1, mw2, function(req, resp) {...

反正。一种替代方法可能是重构您的代码,以便您只有一个处理函数。你有充分的理由让 mw1 和 mw2 成为中间件而不是你的处理程序调用的常规异步函数吗?

var express = require('express');
var server = express();

var mw1 = function(req, res, callback) {
  // do stuff with req/res if necessary but don't send a response
  if (success) {
    callback(null);
  } else {
    callback('Error');
  }
};

var mw2 = function(req, res, callback) {
  //do other stuff but don't send a response
  if (success) {
    callback(null);
  } else {
    callback('Error');
  }
};

function mwBoth(req, res){
  mw1(req, res, function(err){
    if(err){ return res.send(500) };
    mw2(req, res, function(err){
      if(err){ return res.send(500) };
      // neither had an error
      res.redirect('/some/other/path');
    });
  });
};

server.post('/some/path', mwBoth);
于 2013-06-21T14:24:57.930 回答