Skip to content

Instantly share code, notes, and snippets.

@spinscale
Created January 11, 2015 21:43
Show Gist options
  • Save spinscale/27584fd5f7035e31c0ff to your computer and use it in GitHub Desktop.
Save spinscale/27584fd5f7035e31c0ff to your computer and use it in GitHub Desktop.
simple flash scope using expressjs
// poor mans flash scope
// message appears once, but reloading in the browser makes it vanish
app.use(function(req, res, next){
if (req.session.message !== null) {
res.locals.message = req.session.message
req.session.message = null
}
next();
});
// works for rendering
app.get('/', function(req, res) {
req.session.message = "foo bar baz";
res.render('index', { title: 'Express' });
})
// works even for redirects
app.get('/', function(req, res) {
req.session.message = "foo bar baz";
res.redirect('/');
})
/* in jade template
if message
div.ui.blue.message= message
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment