Node.js 在路线之间传递物体(护照)

Node.js 在路线之间传递物体(护照),node.js,express,passport.js,Node.js,Express,Passport.js,我想我正在尝试做的应该是相对容易的,但是我正在失去线索,并且可能失去做这件事的意愿 使用node和express 4设置节点应用程序。我用护照认证。遵循scott.io的绝妙指导,这一技巧做得很好 而且它很有魅力。然而,我想分开我的路线,因为我喜欢保持事物整洁(这是一个谎言,但我打算让谎言继续存在) 我的计划是有四套路线。 api(映射到/api,使用文件./routes/api.js) 索引(映射到/,使用文件./routes/index.js) auth(映射到/auth,跟踪所有身份验证、

我想我正在尝试做的应该是相对容易的,但是我正在失去线索,并且可能失去做这件事的意愿

使用node和express 4设置节点应用程序。我用护照认证。遵循scott.io的绝妙指导,这一技巧做得很好

而且它很有魅力。然而,我想分开我的路线,因为我喜欢保持事物整洁(这是一个谎言,但我打算让谎言继续存在)

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

现在说到我的问题,我需要让passport对应用程序可用(或者让api.js和indes.js能够调用passport.js中的函数),但我不太明白怎么做

我的计划是启动passport,如下所示:

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的超级简单版本)

我的问题是,如果我这样做,express会抱怨

      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”

那么,有没有一种正确、简单或最好是正确、简单的方法来实现这一点呢? 想一想诀窍是传递应用程序和passport(或者只传递passport),想一想我需要从passport访问所有三个应用程序中的数据或函数,因为我也计划使用ACL,所以想将其添加到auth中,以使我的生活变得简单

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

这就是我的问题。 如果我现在发布到身份验证路由(下面的代码)

最后,我发现路由代码(./routes/auth.js)不知道passport是什么。(存放在应用程序中,如下所示):


您将收到错误,因为您没有返回路由器

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

您将无法访问passport属性,因为您没有将其传递或设置在任何位置。由于我不确定passport是如何工作的(无论它是否作为单例),因此您的routes文件中有两个选项:

var passport = require('passport')
这可能“只是工作”,或者

第三个选项是使用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;
}

我没有使用passport,因此无法提供任何帮助,但您将收到错误,因为您没有返回路由器:module.exports=function(app,passport){return router;}。但根据你的问题,你不需要将应用程序传递到函数现在我觉得自己像个傻瓜。我想你已经解决了这个问题,因为你忙于思考passport对象,忘记了返回,这似乎工作得非常完美。可爱,那么我将添加一个完整的答案:)抱歉,提前跳枪,这让node很高兴,但它不允许路由访问对象。已添加编辑我已编辑我的答案。。。
module.exports=function(app, passport) {
    return router;
}
var passport = require('passport')
var passport; // at the top of your routes file

// your routes

module.exports = function(app, _passport) {
    passport = _passport;
    return router;
}
// 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;
}