本文介绍了node.js回调中“done”和“next”的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在护照[配置认证]文档中,它有一个相当可怕的功能,使用神秘的功能完成。in the passport [configure authentication] documentation, it has a rather scary-looking function that uses the mysterious function "done.'passport.use(new LocalStrategy( function(username, password, done) { User.findOne({ username: username }, function (err, user) { if (err) { return done(err); } if (!user) { return done(null, false, { message: 'Incorrect username.' }); } if (!user.validPassword(password)) { return done(null, false, { message: 'Incorrect password.' }); } return done(null, user); }); }));在快速说明文档中,有很多方法可以传递一些名为next的方法。 p>Now, in the express documentation there are quite a few methods that pass something called next.app.use(function(err, req, res, next){ console.error(err.stack); res.status(500).send('Something broke!');});这两个框架之间有区别,快递和护照?或者他们在做两件事吗?Is this the difference between the two frameworks, express and passport? Or are they doing two separate things?推荐答案 这两个框架之间的区别是快递和护照? 没有使用它们的目的不同。 Express用作node.js上的应用程序框架,其中护照只处理Web应用程序的身份验证部分。Is this the difference between the two frameworks, express and passport?No they are different in the purpose for which they are used for. Express is used as a application framework on node.js where as passport just handles the authentication part of a web application. next()是连接的一部分,inturn是一个明确的依赖关系。调用next()的目的是在快速堆栈中触发下一个中间件。next() is part of connect which inturn is an express dependency. The purpose of calling next() is to trigger the next middle ware in express stack.要以更简单的方式了解 next()概念,您可以查看基于 在这里表达 。To understand the next() concept in an easier way, you could look at a sample app built on express here.正如你可以看到的那样,应用程序使用路由级中间件来检查用户是否登录。as you can see in the line pointed the application uses a route level middleware to check whether the user is logged in or not.app.get('/account', ensureAuthenticated, function(req, res){这里ensureAuthenticated是在底部定义的中间件,如Here ensureAuthenticated is the middleware which is defined at bottom likefunction ensureAuthenticated(req, res, next) { if (req.isAuthenticated()) { return next(); } res.redirect('/login')},您可以看到用户是否通过身份验证函数调用 next()并将控件传递到上面写的路由处理器中的下一层,否则重定向到另一层即使没有调用 next()as you can see if the user is authenticated the function invokes next() and passes control to the next layer in route handler written above, else it redirects to another route even without invoking the next() done()用于触发我们为护照身份验证编写的返回URL处理程序。要了解更多关于工作情况的详细信息,可以在 护照在这里查看代码示例 并检查标题为自定义回拨done() on the other hand is used to trigger the return url handlers that we write for passport authentication. To understand more on how done works you could look at the code samples at passport here and check the section titled Custom Callbackapp.get('/login', function(req, res, next) { passport.authenticate('local', function(err, user, info) { if (err) { return next(err); } if (!user) { return res.redirect('/login'); } req.logIn(user, function(err) { if (err) { return next(err); } return res.redirect('/users/' + user.username); }); })(req, res, next);});这里第二个参数为 passport.authenticate 是您从护照策略中调用的 done()的定义。Here the second parameter to passport.authenticate is the definition of done() that you are going to call from the passport strategy.上面提供的两个链接中的示例代码帮助我们了解其行为比文档。我建议你这样做。Here going through the sample codes in the two links I provided above have helped alot in understanding its behavior than the docs. I would suggest you to do the same. 这篇关于node.js回调中“done”和“next”的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 07:12