为什么Passport.js会给我一个中间件错误?

时间:2012-05-08 11:02:40

标签: node.js passport.js

我正在尝试此处所述的解决方法

https://github.com/jaredhanson/passport/issues/14

app.use(passport.initialize());
app.use(passport.session());  
app.use(app.router);
app.use(express.static(__dirname + '/public'));

工作正常

app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(passport.initialize());
app.use(passport.session()); 

给出

DEBUG: Error: passport.initialize() middleware not in use
    at IncomingMessage.<anonymous> (/.../node_modules/passport/lib/passport/http/request.js:30:30)
    at Context.<anonymous> (/.../node_modules/passport/lib/passport/middleware/authenticate.js:92:11)
    at Context.<anonymous> (/.../core/node_modules/passport/lib/passport/context/http/actions.js:21:25)
    at Strategy.success (native)

3 个答案:

答案 0 :(得分:27)

Passport后仍需要app.use(app.router)。否则,您的路由将在任何Passport代码之前运行,这就是您看到错误的原因。这应该有效:

app.use(express.static(__dirname + '/public'));
app.use(passport.initialize());
app.use(passport.session()); 
app.use(app.router);

答案 1 :(得分:0)

可能会帮助某人,我遇到了同样的问题。我的app配置看起来像这样。这导致了错误。

app.configure(function() {
  ....
app.use(app.router);    
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));

});

我必须按照以下方式重新排序才能正常工作。

app.configure(function() {
  ....
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);    
app.use(express.static(path.join(__dirname, 'public')));

});

答案 2 :(得分:0)

最适合我的就是:

app.use(passport.initialize());
app.use(passport.session());

先于其他app.use