[英]Understanding next middle-ware in the scope of express.js / sail.js controller
I am using sails.js which is built on top of express.js . 我正在使用建立在express.js之上的sails.js。 In my routes.js I defined a route like following
在我的routes.js中,我定义了一条如下路线
'/account/login': {
controller : 'Session',
action : 'logInAPI'
},
And my session controller is like this 我的会话控制器是这样的
logInAPI: function(req, res, next) {
if (!req.param('email') || !req.param('password')) {
var usernamePasswordRequiredError = [
'You must enter a username and a password.'
]
req.session.flash = {
err: usernamePasswordRequiredError
}
return next();
}
else{// Log in user};
}
I am wondering when I am calling next where does it pointing to in middlewear. 我想知道我什么时候打电话给下一个指向中间装的地方。 What will happen if just write return;
如果只写回来会发生什么; Instead of return next();
而不是返回next();
The reason you call next()
instead of just returning some value is to allow asynchronous serial chaining of middleware operations. 调用
next()
而不是仅返回一些值的原因是允许异步串行链接中间件操作。 For example: 例如:
function(req, res, next){
setTimeout(next, 1000);
}
This is an example of a useless middleware that does nothing except delay the response for one second. 这是一个无用的中间件的例子,除了延迟响应一秒钟之外什么都不做。 Note specifically that in this case,
next()
executes after the function returns. 特别注意,在这种情况下,
next()
在函数返回后执行。
In a more realistic scenario, setTimeout()
would of course be replaced by some logic that does a database fetch and accepts a callback, for example. 在更现实的情况下,例如,
setTimeout()
当然会被一些执行数据库提取并接受回调的逻辑所取代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.