Error: req.flash() requires sessions

Error: req.flash() requires sessions

我是 node 的新手,我很确定我已经设置了中间件并表示使用 flash 消息传递,但是我仍然收到错误消息:

Error: req.flash() requires sessions

设置

//express.js
     var flash = require('connect-flash')

     module.exports = function (app, config, passport) {
         app.use(flash());
     };

//route js
     exports.loginGet = function (req, res) {
       res.render('users/login', {
         title: 'Login',
         message: req.flash('error') //error in question
       });
     };

我还能做些什么来确保一切设置正确并正常运行?

来自 readme(强调我的):

Flash messages are stored in the session. First, setup sessions as usual by enabling cookieParser and session middleware. Then, use flash middleware provided by connect-flash.

express-sessions 与 express 4 结合使用,不再需要 cookieParser

var session = require('express-session');

//...


app.use(session({ cookie: { maxAge: 60000 }, 
                  secret: 'woot',
                  resave: false, 
                  saveUninitialized: false}));

请检查 mongodb 连接。可能会出现 mongo 错误,例如 "mongoError: Topology was destroyed"。 要解决此问题,请检查

在我的例子中,问题是 Redis 没有在监听。我通过启用 logErrors 属性:

发现了这一点
new RedisStore({
  host: 'localhost',
  port: '6379',
  logErrors: true,
});

这导致了如下消息:

Warning: connect-redis reported a client error: Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379

我遇到了这些问题,我通过尊重级联来解决它们

app.use(passport.initialize());
app.use(passport.session());
//SESSION FLASH
app.use(flash());