【问题标题】:connect-flash middleware not working连接闪存中间件不工作
【发布时间】:2012-11-07 09:06:14
【问题描述】:

我正在尝试让 connect-flash 在我的 express3 应用程序中工作。

我成功安装了包:

$ npm install connect-flash

我包含了它:

var flash = require('connect-flash');

设置中间件:

app.use(function(req, res, next) {
    res.locals.message = req.flash();
    next();
  });
app.use(flash());

并使用它:

app.get('/admin', function(req, res) {   
    if(loggedIn === true) {      
      res.redirect('/admin/books');
    }
    else {      
      res.render('login', {message: req.flash('error') });
    }    
  });
  app.post('/admin', function(req, res) {    
    if((adminAccount.username === getCrypted(req.body.username)) && 
      (adminAccount.password === getCrypted(req.body.password))) {

      loggedIn = true;
      res.redirect('/admin/books');
    }
    else {
      req.flash('error', 'Woops, looks like that username and password are incorrect.');
      res.redirect('/admin');
    }
  });

但是我得到:TypeError: Object #<IncomingMessage> has no method 'flash'。我按照其 github 页面上的说明进行操作。我错过了什么?

【问题讨论】:

    标签: node.js express


    【解决方案1】:

    颠倒顺序:

    app.use(flash());
    
    app.use(function(req, res, next) {
      res.locals.message = req.flash();
      next();
    });
    

    【讨论】:

      猜你喜欢
      • 2017-10-29
      • 2016-08-24
      • 1970-01-01
      • 2012-09-25
      • 2018-04-15
      • 2014-01-13
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多