Last active
August 29, 2015 14:01
-
-
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();
This file contains 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
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