6

使用 Node.js 和 Express 时出现以下错误。这是我的版本:

Node : v0.8.8
Express: 3.0.0rc3

尝试访问页面时出现以下错误:

TypeError: Object #<IncomingMessage> has no method 'flash'
    at routes (/Users/nblavoie/Desktop/HotPie/HotPie/apps/authentication/routes.js:23:8)

我试图删除 app.use(app.router); 从 app.js 没有成功。这是导致错误的代码。请参阅 req.flash 行。

app.post('/sessions', function(req, res)
{
    if(req.body.user == 'piechief' 
        && req.body.password == '12345')
    {
        req.session.currentUser = req.body.user;
        req.flash('info', "You're logged in as #{req.session.currentUser}");
        res.redirect("/login");
        return;
    }
    else
    {
        req.flash('error', "Those credentials were incorrect. Try again.");
        res.redirect("/login");
        return;
    }
});

这是我的 app.js 中有趣的部分:

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({
    secret: "DBBD6BE563419EDB0E5CBD228E603D4AD232CE05434B4FA95C6908B64EA515C3",
    store: new RedisStore({
      host: "127.0.0.1",
      port: "6379",
      db: "mydb"
    })
  }));
  app.use(app.router);
  app.use(express.static(path.join(__dirname, '/public')));
});

请问有什么想法吗?我看了一下Express'flash 文档,但看不出我做错了什么。

4

2 回答 2

25

req.flash自3.0起已被删除:

  • req.flash()(只使用会话:req.session.messages = ['foo']或类似的)
  • connect-flash可以用作提供 req.flash() 的中间件
于 2012-08-25T12:23:55.953 回答
5

这为我解决了

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

现在我的护照 app.configure 看起来像这样:

app.configure(function (){
    app.use(express.cookieParser('keyboard cat'));
    app.use(express.session({ cookie: { maxAge: 60000 }}));
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(flash());
})
于 2014-03-13T14:56:20.150 回答