11

I'm new to node and I'm pretty sure I've set up the middle ware and express to use flash messaging however I still get the error:

Error: req.flash() requires sessions

Setup

//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
       });
     };

What else can I do to make sure I have everything set up correctly and get it working?

1

4 Answers 4

15

From the readme (emphasis mine):

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.

Using express-sessions with express 4, cookieParser is no longer required.

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

//...


app.use(session({ cookie: { maxAge: 60000 }, 
                  secret: 'woot',
                  resave: false, 
                  saveUninitialized: false}));
2
  • isn't using express.XXX from express v2 and 3? I thought they removed this features from express and made them single middleware? Jun 1, 2015 at 9:09
  • My bad - you are correct. I have updated my answer accordingly.
    – andars
    Jun 1, 2015 at 15:47
3

In my case the issue was that Redis was not listening. I found that out by enabling the logErrors property:

new RedisStore({
  host: 'localhost',
  port: '6379',
  logErrors: true,
});

Which resulted in messages like these:

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

Please check mongodb connections. there may be an mongo error like "mongoError: Topology was destroyed". To fix this issue, check here

1
  • This helped me fix the same error message caused by a misconfigured (wrong host name) using connect-redis session
    – Crisp
    Oct 22, 2018 at 7:15
1

i was having these issues and I solve them by respecting the cascading

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.