Skip to content

Instantly share code, notes, and snippets.

@jeromeetienne
Created June 1, 2011 10:48
Show Gist options
  • Save jeromeetienne/1002105 to your computer and use it in GitHub Desktop.
Save jeromeetienne/1002105 to your computer and use it in GitHub Desktop.
// attempts to get flash/session accessible in any template
// - this middleware function fails and keep producing 'Error: req.flash() requires sessions'
// - this middleware has been place *after* session middleware in the stack
app.use(function(req,res, next){
console.log("flash middleware")
res.locals({
session : req.session,
flash : req.flash()
});
});
@alejandro
Copy link

Did you fix the Error: req.flash() requires sessions ? I don't know how to fix this error.

@jeromeetienne
Copy link
Author

i think you need session in the express config. http://expressjs.com/guide.html#session-support

and before using flash, as shown in the example

@alejandro
Copy link

Yeah! Actually I'm doing this...
var MemStore = require('connect').session.MemoryStore; app.configure(function() { app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.logger()); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(__dirname + '/public')); app.use(express.cookieParser()); app.use(express.session({ store: new MemStore({ reapInterval: 60000 * 10 }) , secret:"alejandromg"})); });

@mhuggins
Copy link

I'm getting this error too despite including the following in my code:

app.use(express.session({secret: 'foobar'}));

Any luck getting it to work?

@alejandro
Copy link

@mhuggins Basically, what I've ended doing is this:

var app = express.createServer(
  express.bodyParser(),
  express.cookieParser('secretKey'),
  express.session({secret: 'secretKey'})
 );

Then:

app.dynamicHelpers({
   flash: function(req,res){
     return req.flash();
   } 
});

Check that I'm not passing those params inside app.configure(), I'm setting that when I create the express server.

@benbonnet
Copy link

@AlejandroMG, thx for the tip; works well

@andreydjason
Copy link

thanks @AlejandroMG, I'm was with "req.flash() requires sessions" error too, and this works for me as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment