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.
npm install sails-hook-flash
Use --save
to add in your package.json.
Adding a new flash message:
// api/controllers/SampleController.js
login: function (request, response) {
request.addFlash('success', 'A success message.');
return response.redirect('/sample/success');
}
Rendering your flash message:
// views/sample/success.ejs
<% flash.get('success').forEach(function (message) { %>
<div style="color:green;">
<%= message %>
</div>
<% }) %>
NOTE: The EJS view engine is used in this sample, but you're not limited to it.
request.addFlash(type, message)
- Stores a new messagerequest.getFlash(type)
- Returns all messages from a typerequest.hasFlash(type)
- Checks if a message type was stored
flash.all()
- Returns all messagesflash.get(type)
- Returns all messages from a typeflash.has(type)
- Checks if a message type was stored