express-flasher/README.md
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

1.2 KiB

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.