Express.js middleware to set and retrieve session flash messages
Go to file
Gregory Ballantine db9ff7ef05
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
Added the code for the express-flasher; added ESLint; added Woodpecker CI config
2022-11-04 16:09:20 -04:00
.eslintrc.json Added the code for the express-flasher; added ESLint; added Woodpecker CI config 2022-11-04 16:09:20 -04:00
.gitignore Initial commit 2022-11-04 15:20:29 -04:00
.woodpecker.yml Added the code for the express-flasher; added ESLint; added Woodpecker CI config 2022-11-04 16:09:20 -04:00
index.js Added the code for the express-flasher; added ESLint; added Woodpecker CI config 2022-11-04 16:09:20 -04:00
LICENSE Added the code for the express-flasher; added ESLint; added Woodpecker CI config 2022-11-04 16:09:20 -04:00
package-lock.json Added the code for the express-flasher; added ESLint; added Woodpecker CI config 2022-11-04 16:09:20 -04:00
package.json Added the code for the express-flasher; added ESLint; added Woodpecker CI config 2022-11-04 16:09:20 -04:00
README.md Added the code for the express-flasher; added ESLint; added Woodpecker CI config 2022-11-04 16:09:20 -04:00

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.