Skip to content

Instantly share code, notes, and snippets.

@brooklynDev
Last active August 29, 2015 14:01
Show Gist options
  • Save brooklynDev/661384d07867ca293bee to your computer and use it in GitHub Desktop.
Save brooklynDev/661384d07867ca293bee to your computer and use it in GitHub Desktop.
Out of the box, express-flash only supports strings, occasionally it's useful to have some json stick around after redirects. This middleware tacks on a jsonFlash function to accomplish that. If you require this middleware you have both flash() and jsonFlash();
var flash = require('express-flash')();
module.exports = function() {
return function(req, res, next) {
flash(req, res, function() {
req.jsonFlash = function() {
if (arguments.length === 1) {
var data = req.flash(arguments[0]);
if (data.length) {
return JSON.parse(data);
}
} else {
var key = arguments[0];
var value = JSON.stringify(arguments[1]);
req.flash(key, value);
}
};
});
next();
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment