javascript node.js next()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5384526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript node.js next()
提问by Harry
I see a lot of use next
in node.js.
我next
在 node.js 中看到了很多用途。
What is it, where does it come from? What does it do? Can I use it client side?
它是什么,它来自哪里?它有什么作用?我可以在客户端使用它吗?
Sorry it's used for example here: http://dailyjs.com/2010/12/06/node-tutorial-5/
抱歉,这里使用了例如:http: //dailyjs.com/2010/12/06/node-tutorial-5/
look for the loadUser function.
寻找 loadUser 函数。
采纳答案by Wayne
This appears to be a variable naming convention in Node.js control-flow code, where a reference to the next function to execute is given to a callback for it to kick-off when it's done.
这似乎是 Node.js 控制流代码中的变量命名约定,其中对要执行的下一个函数的引用被赋予回调,以便在它完成时启动。
See, for example, the code samples here:
例如,请参阅此处的代码示例:
Let's look at the example you posted:
让我们看看您发布的示例:
function loadUser(req, res, next) {
if (req.session.user_id) {
User.findById(req.session.user_id, function(user) {
if (user) {
req.currentUser = user;
return next();
} else {
res.redirect('/sessions/new');
}
});
} else {
res.redirect('/sessions/new');
}
}
app.get('/documents.:format?', loadUser, function(req, res) {
// ...
});
The loadUser
function expects a function in its third argument, which is bound to the name next
. This is a normal function parameter. It holds a reference to the next action to perform and is called once loadUser
is done (unless a user could not be found).
该loadUser
函数在其第三个参数中需要一个函数,该参数绑定到 name next
。这是一个正常的函数参数。它保存对要执行的下一个操作的引用,并loadUser
在完成后调用(除非找不到用户)。
There's nothing special about the name next
in this example; we could have named it anything.
next
这个例子中的名字没有什么特别之处;我们可以给它起任何名字。
回答by Pero P.
It is naming convention used when passing callbacks in situations that require serial execution of actions, e.g. scan directory -> read file data -> do something with data. This is in preference to deeply nesting the callbacks. The first three sections of the following article on Tim Caswell's HowToNodeblog give a good overview of this:
这是在需要串行执行操作的情况下传递回调时使用的命名约定,例如扫描目录 -> 读取文件数据 -> 对数据执行某些操作。这优先于深度嵌套回调。Tim Caswell 的HowToNode博客上的以下文章的前三个部分对此进行了很好的概述:
http://howtonode.org/control-flow
http://howtonode.org/control-flow
Also see the Sequential Actionssection of the second part of that posting:
另请参阅该帖子第二部分的顺序操作部分:
回答by Alireza
It's basically like a callback that express.jsuse after a certain part of the code is executed and done, you can use it to make sure that part of code is done and what you wanna do next thing, but always be mindful you only can do one res.send
in your each RESTblock...
它基本上就像是express.js在执行并完成某部分代码后使用的回调,您可以使用它来确保部分代码已完成以及您接下来想要做什么,但请始终注意您只能res.send
在你的每个REST块中做一个...
So you can do something like this as a simple next()
example:
所以你可以做这样的事情作为一个简单的next()
例子:
app.get("/", (req, res, next) => {
console.log("req:", req, "res:", res);
res.send(["data": "whatever"]);
next();
},(req, res) =>
console.log("it's all done!");
);
It's also very useful when you'd like to have a middleware in your app...
当您希望在您的应用程序中有一个中间件时,它也非常有用...
To load the middleware function, call app.use(), specifying the middleware function. For example, the following code loads the myLogger middleware function before the route to the root path (/).
要加载中间件函数,请调用 app.use(),指定中间件函数。例如,以下代码在路由到根路径(/)之前加载 myLogger 中间件函数。
var express = require('express');
var app = express();
var myLogger = function (req, res, next) {
console.log('LOGGED');
next();
}
app.use(myLogger);
app.get('/', function (req, res) {
res.send('Hello World!');
})
app.listen(3000);