50 lines
1.2 KiB
Markdown
50 lines
1.2 KiB
Markdown
# 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](https://www.npmjs.com/package/express-session) 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!):
|
|
|
|
```javascript
|
|
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:
|
|
|
|
```javascript
|
|
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:
|
|
|
|
```twig
|
|
...
|
|
{% 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.
|