express-flasher/index.js

41 lines
905 B
JavaScript
Raw Normal View History

// used to set a flash message for next request
exports.flash = function() {
return function(req, res, next) {
if (req.flash) {
return next();
}
req.flash = _flash;
next();
};
};
// used on new requests to check for flash messages stored in session
exports.flashRead = function() {
return function(req, res, next) {
if (typeof req.session.flash !== 'undefined') {
res.locals.flash = req.session.flash;
}
next();
};
};
/**
* Sets a session-based flash message.
*
* @this req
* @param {string} type - user-defined type of flash message
* @param {string} msg - message to be displayed to the user
*/
function _flash(type, msg) {
// verify that express-session has been enabled
if (this.session === undefined) throw Error('req.flash() requires sessions');
// set session properties
this.session.flash = {
type: type,
msg: msg,
};
}