Express.js middleware to set and retrieve session flash messages
Gregory Ballantine
129f52bfa4
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
|
||
---|---|---|
.eslintrc.json | ||
.gitignore | ||
.woodpecker.yml | ||
index.js | ||
LICENSE | ||
package.json | ||
README.md |
express-flasher
Express.js middleware to set and retrieve session flash messages
Requirements
For the express-flasher
module to work, you must first have the express-session
module enabled in your express app. Please refer to the express-session documentation on how to set it up.
Installation
Installating this module is super easy with NPM:
npm install --save express-flasher
Usage
After the module is installed, you can set up the express-flasher by registering the middleware (both middlewares are important!):
const flasher = require('express-flasher');
...
app.use(flasher.flash()); // enables the req.flash() method for setting flash messages
app.use(flasher.flashRead()); // reads flash messages and adds it to the view
Now you can set messages in your routes:
exports.index = function(req, res) {
flash('info', 'This is a flash!');
res.render('index.twig');
};
And finally, you can read the flash messages in your views:
...
{% if flash %}
<div class="flash-message {{ flash.type }}">
<p>{{ flash.msg }}</p>
</div>
{% endif %}
...
License
This project is licensed under the BSD 2-Clause license.