调整中间件 – 正确调用堆栈中的下一个中间件

我正在使用Restify与Nodejs,我有一个正确的方式将控制返回到堆栈中的下一个中间件的问题。 我希望当我说“堆栈中的下一个中间件”时使用正确的短语。

基本上,我的代码如下所示:

//server is the server created using Restify server.use(function (req, res, next) { //if some checks are a success return next(); }); 

现在,我想知道的是该代码应该return next(); 或者应该只是next(); 把控制权交给下一个堆栈?

我检查了两个工作 – 这两个代码块将成功地传递控制权,并按预期返回数据 – 我想知道的是,如果两者之间存在差异,并且如果我需要使用另一个。

没有区别。 我看了一下Restify的源码,似乎没有对中间件的返回值做任何事情。

使用return next()的原因纯粹是为了方便:

 // using this... if (someCondition) { return next(); } res.send(...); // instead of... if (someCondition) { next(); } else { res.send(...); }; 

这可能有助于防止这样的错误:

 if (someCondition) next(); res.send(...); // !!! oops! we already called the next middleware *and* we're // sending a response ourselves!