express-simple-flash

Allows to use flash object in views

npm install express-simple-flash
2 downloads in the last week
5 downloads in the last month

express-simple-flash

This module allows you to use flash object in your views.

Installation

$ npm install express-simple-flash --save

Usage

template.yourengine

<% if flash.error %>
  <p>
      <% echo flash.error %>
  </p>
<% end if %>
<form action="/some_action" method="post">
  <input type="text" name="some_field"><br>
  <% if flash.errors.some_field %>
    <% echo flash.errors.some_field.join(';') %>
  <% end if%>
  <input type="submit" value="Act!">
</form>

app.js

var express = require('express')
  , simple_flash = require('express-simple-flash')

var app = express()

// default_obj = {
//   error: null,
//   errors: {},
//   info: null,
//   infos: {}
// }

app.all('*', simple_flash.flash(/* default_obj */)) // app.use(simple_flash.flash()) doesn't work. Why?

app.get('/', function(req, res) {
  res.render('template')
})

app.post('/some_action', function(req, res) {
   req.session.flash.error = 'Example error'
   req.session.flash.errors.some_field = ['Example field error']
   res.redirect('/')
})

app.listen(3000)
npm loves you