在路由之间传递对象(护照)

想想我要做的事情应该是相对容易的,但是我放弃了这个线索,并且有潜力去做这件事。

使用节点设置节点应用程序并快速4.我使用护照进行身份validation。 跟着一个绝对惊人的指导scott.io很好地https://scotch.io/tutorials/easy-node-authentication-setup-and-local

这是一种魅力。 不过,我想分开我的路线,因为我喜欢保持整洁(这是一个谎言,但我打算保持谎言的生活)。

我的计划是有四套路线。 api(映射到/ api,使用文件./routes/api.js)索引(映射到/,使用文件./routes/index.js)auth(映射到/ auth,跟踪所有身份validation,callback为以及一些激活和其他位)

现在我的问题,我需要使护照可用的应用程序(或获得api.js和indes.js能够调用passport.js函数),我不能完全弄清楚如何。

我的计划是像这样发起护照:

var passport = require('passport'); app.use(session({secret: 'Not-telling-you)', saveUninitialized: true, resave: true })); // session secret app.use(passport.initialize()); app.use(passport.session()); // persistent login sessions app.use(flash()); // use connect-flash for flash messages stored in session //Configuring the passports require('./config/passport')(passport); 

这应该给我护照在应用程序中可用

接下来加载路由模块

 var auth = require('./routes/auth')(app, passport); var users = require('./routes/users')(app,passport); var activator = require('./routes/activator')(app,passport); 

这应该让我在模块中访问它们?

地图在应用程序中的所有toutes

 app.use('/api', api); app.use('/auth', auth); app.use('/', index); 

然后写模块如下(这是一个超级简单的auth版本)

 var bodyParser = require('body-parser'); var activator = require('activator'); var express = require('express'); var router = express.Router(); //Lets read the configuration files we need var activatorCfg = require('../config/activator.js') var cfgWebPage = require('../config/webpage.js'); //So we can read the headers easily router.use(bodyParser.json()); // support json encoded bodies router.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies //Activating activator, so we can actively activate the actives activator.init({user: activatorCfg, transport: activatorCfg.smtpUrl , from: activatorCfg.fromEmail, templates: activatorCfg.templatesDir}); router.get('/login', function(req, res) { res.render('login.ejs', { title: 'Betchanow - Social betting as it should be' , loginUrl: cfgWebPage.loginUrl, trackingID: cfgWebPage.googleTracking.trackingID, message: req.flash('loginMessage') }); }); module.exports=function(app, passport) { router } 

我的问题是,如果我这样做,表示抱怨

  throw new TypeError('Router.use() requires middleware function but got a ^ TypeError: Router.use() requires middleware function but got a undefined 

如果我只是返回路由器(跳过包装它在一个函数),我结束了一个
var search = 1 + req.url.indexOf('?'); ^ TypeError:无法读取未定义的属性'indexOf'

那么有没有一个正确的,简单的或最好的正确和简单的方法来实现这一目标? 认为诀窍是通过应用程序和护照(或只有护照),认为我需要从护照中访问数据或function的三个,并且正如我打算与ACL玩,以及要添加到auth让我的生活变得简单

==============编辑=============

所以这是我的问题。 如果我现在在authentication路线上发帖(下面的代码)

 //Lets load the modules, note the missing passport var bodyParser = require('body-parser'); var activator = require('activator'); var express = require('express'); var router = express.Router(); //Lets read the configuration files we need var activatorCfg = require('../config/activator.js') var cfgWebPage = require('../config/webpage.js'); //So we can read the headers easily router.use(bodyParser.json()); // support json encoded bodies router.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies //Activating activator, so we can actively activate the actives activator.init({user: activatorCfg, transport: activatorCfg.smtpUrl , from: activatorCfg.fromEmail, templates: activatorCfg.templatesDir}); //Lets start with our routes // process the login form router.post('/login', passport.authenticate('local-login', { successRedirect : '/', // redirect to the secure profile section failureRedirect : '/login', // redirect back to the signup page if there is an error failureFlash : true // allow flash messages })); module.exports=function(app, passport) { return router; } 

我最终的问题是路由代码(./routes/auth.js)不知道护照是什么。 (在应用程序中如下所示):

 app.use(passport.initialize()); app.use(passport.session()); // persistent login sessions app.use(flash()); // use connect-flash for flash messages stored in session //Configuring the passports require('./config/passport')(passport); 

你会得到错误,因为你没有返回路由器。

 module.exports=function(app, passport) { return router; } 

编辑:

你不能访问护照的财产,因为你没有通过或设置在任何地方。 由于我不确定护照是如何工作的(不pipe它是否是单身人士),所以你在路线文件中有几个选项:

 var passport = require('passport') 

这可能“正常工作”,或者

 var passport; // at the top of your routes file // your routes module.exports = function(app, _passport) { passport = _passport; return router; } 

第三个选项是将您的整个路线包裹在exports方法中:

 // your requires here module.exports = function(app, passport) { //So we can read the headers easily router.use(bodyParser.json()); // support json encoded bodies router.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies //Activating activator, so we can actively activate the actives activator.init({user: activatorCfg, transport: activatorCfg.smtpUrl , from: activatorCfg.fromEmail, templates: activatorCfg.templatesDir}); //Lets start with our routes // process the login form router.post('/login', passport.authenticate('local-login', { successRedirect : '/', // redirect to the secure profile section failureRedirect : '/login', // redirect back to the signup page if there is an error failureFlash : true // allow flash messages })); return router; }