The simplest way to use flash messages in your Sails application. This is useful when you want to redirect and have a special message shown on the next page.
Add a new flash message
// api/controllers/UserController.js
login: function (req, res) {
req.addFlash('success', 'A success message');
return res.redirect('/sample/success');
}
Render your flash message
// views/admin/index.ejs
<% flash.get('success').forEach(function (message) { %>
<div class="alert alert-success">
<%= message %>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<% }) %>
NOTE: The EJS view engine is used in this sample, but you're not limited to it.
req.addFlash(type, message)
- Store a new flash messagereq.getFlash(type
- Get a specific type of flash messagesreq.hasFlash(type
- Check if exists a specific type of flash message
flash.all()
- Get all flash messagesflash.get(type)
- Get a specific type of flash messagesflash.has(type)
- Check if exists a specific type of flash message
npm install sails-hook-flash
Use --save
to add in your dependencies