如何将configuration的护照对象传递到Express4中的路由模块?

因为从Express 4你不应该这样做

require('./app/routes.js')(app, passport); // load our routes and pass in our app and fully configured passport module.exports = function(app, passport) { // ===================================== // FACEBOOK ROUTES ===================== // ===================================== // route for facebook authentication and login app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' })); // handle the callback after facebook has authenticated the user app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect : '/profile', failureRedirect : '/' })); // route for logging out app.get('/logout', function(req, res) { req.logout(); res.redirect('/'); }); }; 

相反,你应该使用express.Route()函数和

 var routes = require('./app/routes.js'); app.use('/', routes); 

如何将configuration的护照传递到Express 4中的路由模块?

       

网上收集的解决方案 "如何将configuration的护照对象传递到Express4中的路由模块?"

function导出仍可用于在模块之间传递passport参考。 它只会创build并return一个Router而不是直接修改app

 var express = require('express'); module.exports = function(passport) { var router = express.Router(); router.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' })); // etc. return router; }; 

然后, app可以use它:

 var routes = require('./app/routes.js')(passport); app.use('/', routes);