nodejs / express - 无法使用护照加载路由

时间:2014-03-10 10:02:25

标签: node.js authentication express passport.js

我正在尝试使用护照在我的webapp上添加用户会话。我已将主js文件配置如下:

 /**
 * Module dependencies.
 */
var express     = require('express');
var user        = require('./server/routes/user');
var routes      = require('./server/routes/index');
var http        = require('http');
var path        = require('path');
var app         = express();
var passport    = require('passport');
var flash       = require('connect-flash');

require('./server/config/passport')(passport); // pass passport for configuration

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));

app.configure(function(){
    // required for passport
    app.use(express.session({ secret: 'ilovesefounders' })); // 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
})


// routes ======================================================================
require('./server/routes/index')(app, passport); // load our routes and pass in our app and fully configured passport
routes.mount(app);

然而,当我尝试启动它时,我收到以下错误:

require('./server/routes/index')(app, passport); // load our routes and pass i
                                ^
TypeError: object is not a function
    at Object.<anonymous> (/Path/app.js:38:33)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

这里出了什么问题?

1 个答案:

答案 0 :(得分:3)

我不知道你的index.js是怎么样的,但你可能没有导出你的功能模块。

检查index.js是否如下所示:

module.exports = function(app, passport) {
    /* code */
};

而且您不需要再次要求“./server/routes/index”。