Jamie Hutber Jamie Hutber - 4 months ago 193
Node.js Question

Error: req.flash() requires sessions

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?

Answer

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