express-fm v1.0.2
#express-fm Flash message middleware for Express and Connect.
Flash messages are messages that are displayed once when browsing on a website, usually to inform the user of success or error of an operation.
express-fm allows to set flash messages with the function req.flash
. With Express, flash messages are automatically available in locals
variable of the template thanks to an overriding of the res.render
function.
Installation
npm install express-fm
Usage
Initialization
var flash = require('express-fm');
app.use(session(options));
app.use(flash());
You can use many options:
app.use(flash({
secure: true,
wrapRender: true,
localsKey: 'flash',
sessionKey: 'flash'
}));
Options:
key | type | default | description |
---|---|---|---|
secure | Boolean | true | If security is disabled (false ) and middleware's called a second time, it will update flash message's options. |
wrapRender | Boolean | true | If set to true middleware will override res.render function for automatically make available flash messages in locals variable of the template. |
localsKey | String | 'flash' | The key of flash messages in locals variable of the template. |
sessionKey | String | 'flash' | The key of flash messages in req.session . |
Set flash messages
app.get(path, function (req, res) {
// Basic
req.flash('info', 'Information message.');
// Message array
req.flash('info', ['First message.', 'Second message.']);
// Format
req.flash('info', 'Information: %s.', info);
res.redirect('/');
});
Get flash messages
If you use Express, flash messages are automatically available in locals
variable of the template. With Jade template engine, you can display them like that:
if locals.flash
each key in flash
each msg in index
li=msg
But you can also get flash messages manually:
app.get(path, function (req, res) {
var msgs;
// Get only flash messages with 'info' key
msgs = req.flash('info');
// Get all flash messages
msgs = req.flash();
// Send messages to the template
res.render(template, {
flash: msgs
}, null, false);
});
If middleware has overrided res.render
function, it has added him a parameters to disable the automatic sending of flash messages in the template.
Test
npm test
Release History
20/08/2015 1.0.2 Readme completion.
19/08/2015 1.0.1 res.render overriding.
18/08/2015 1.0.0 Initial release.