Created
January 11, 2015 21:43
-
-
Save spinscale/27584fd5f7035e31c0ff to your computer and use it in GitHub Desktop.
simple flash scope using expressjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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