[英]Need help understanding how NodeJS / PassportJS works
I have the following 3 fragments of code taken from a working project I downloaded from internet: 我有以下3个代码片段,这些片段取自从Internet下载的一个工作项目:
file: ./routes.js 文件: ./ routes.js
// ... var passport = require('passport'); var requireLoginLocal = passport.authenticate('local', { session: false }); var authController = require('./controllers/authController'); // ... module.exports = function(app) { // ... authRoutes.post('/login/local', requireLoginLocal, authController.login); // ... }
file: ./config/passport.js 文件: ./ config / passport.js
// ... var LocalStrategy = require('passport-local').Strategy; // ... module.exports = function(passport) { passport.use(new LocalStrategy({ usernameField: 'email' /* changing default */ }, function(email, password, done) { email = email.toLowerCase(); User.findOne({ 'email': email }, function(err, user) { if (err) { return done(err); } if (!user) { return done(null, false, { error: 'Login failed. Please try again.' }); } user.comparePassword(password, function(err, isMatch) { if (err) { return done(err); } if (!isMatch) { return done(null, false, { error: 'Login failed. Please try again.' }); } return done(null, user); }); } ); } )); };
file: ./controllers/authController.js 文件: ./ controllers / authController.js
// ... exports.login = function(req, res, next) { var userInfo = getUserInfo(req.user); res.status(200).json({ token: 'JWT ' + generateToken(userInfo), user: userInfo }); } // ...
I have the following questions: 我有以下问题:
1- On the file: ./routes.js
, on the line: 1-在文件: ./routes.js
,在以下行:
authRoutes.post('/login/local', requireLoginLocal, authController.login);
how the data is passed from the 2nd to the 3rd argument? 数据如何从第二个参数传递到第三个参数?
2- On the file: ./config/passport.js
, how affect those return values to the values passed from the 2nd to the 3rd argument on the line above? 2-在文件./config/passport.js
,这些返回值对上一行从第二个参数传递到第三个参数的值有何影响?
3- On file: ./controllers/authController.js
, on function: exports.login
, nothing is returned there, so, how that affects the values passed from the 3rd argument and a possible 4th argument on an hypothetical line like the one above? 3-在文件: ./controllers/authController.js
,在功能: exports.login
,返回什么都没有,所以,如何影响从第三个参数和可能的第四个参数传递类似上面的假想线的值?
Your 1st and 2nd question are linked. 您的第一个和第二个问题已链接。 This is the middleware pattern used by node specifically express in this case. 这是节点在这种情况下专门表达的中间件模式。 This arcticle explains this very nicely IMO. 这个细节很好地解释了IMO。
In my understanding and simple terms, they are functions which pass around the req, res objects ( info here ), by adding to them with their own changes. 以我的理解和简单的术语来说,它们是通过添加自己的更改而传递给req,res对象( 此处为info )的函数。
In your code you requireLoginLocal will add to the response object whatever it needs and calls next, in which case node will pass the response object to authController.login and so on. 在您的代码中,requireLoginLocal会根据需要添加到响应对象,然后调用它,在这种情况下,节点会将响应对象传递给authController.login,依此类推。 When all the middleware in the chain have done their necessary changes then the final response is send as http response 当链中的所有中间件都进行了必要的更改后,最终响应将作为http响应发送
As for your 2nd question, it's probably used by passport for configuration. 至于第二个问题,护照可能会将其用于配置。 The actual middleware for passport middleware is provide by this line 此行提供护照中间件的实际中间件
passport.authenticate('local', { session: false });
As this a deep and underlying base topic for node, you can google for node middleware and can find tons of articles out there about this 由于这是节点的基础知识,因此您可以在Google上搜索节点中间件,并且可以找到大量有关此的文章。
Answers to your questions: 您的问题的答案:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.