13

我正在尝试这里描述的解决方法

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)
4

3 回答 3

28

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

app.use(express.static(__dirname + '/public'));
app.use(passport.initialize());
app.use(passport.session()); 
app.use(app.router);
于 2012-05-08T13:14:02.763 回答
0

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

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')));

});
于 2016-01-02T20:19:26.690 回答
0

对我有用的是:

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

在任何其他之前app.use

于 2020-04-30T23:05:47.773 回答