Search Courses

Search Courses
Home / Blog
node.js persisting data with flash messages

node.js persisting data with flash messages

Posted on : 24,Mar 2014 3359

Recently I have been working on a node.js project which needed to be more interactive and give the end user lot of messages and alerts , the way google does. I was very consistently using flash message feature in node.js to get the job done. Flash messages is a way to persist data across views without storing data in a file or database. This can be compared to flash messages or session variables in php. Flash data persistence is a very simple technique and you can use it in existing websites also without  disrupting  the routing logic.

In express framework this can be done by enabling session support. Is it an overhead transmission, not so much , just add couple of lines in your configuration section of app.js

To start with,install connect flash

npm install connect-flash

Include and configure flash in app.js


var flash = require('connect-flash');
app.use(express.cookieparser());
app.use(express,session({secret:"OzhclfxGp956SMjtq}));
app.use(flash());


First line says express to use cookieparser which enable express to save sessions. Second line is for encrypting or securing the session. You can generate the secret random number from here

Once session is enabled flash messages can be set within routes


Setting flash message

req.flash('info','Successful');
req.flash('error','Oops something went wrong');


This messages can be included just before redirect or routing


function findLogin(req, res,next) {
        Login.find({'email':req.body.email,'password':req.body.password },function(err, profile) {
              req.session.email = req.body.email;
              if(uses.length !== 0) {
                    req.students = profile;
                     req.flash('info','Log In Successful');
                     res.redirect('/profile')
                }
              res.render('incorrect_login');                              
                }); 
        }


In above example I am trying to log in and once log in is successful before redirecting to profile page , flash message is set.

Now how to display the message in profile page?

To show a  flash message jade template check the existence of flash message and display if available.


- if(typeof flash!= 'undefined')
  - if(flash.warning)
      .alert-message.warning
          p=flash.warning
  - if(flash.info)
      .alert-message.success
          p=flash.info


As a coding standard flash messages can be rendered separately in other views like mixin. Thus code can be reused across views. Confused ?

mixin file is nothing but a jade which is saved along with other views , may be as mixin.jade


mixin flash-messages(flash)
    - if(flash.warning)
      .alert-message.warning
          p=flash.warning
    - if(flash.info)
      .alert-message.success
          p=flash.info


Now this can be reused across views. Add below lines of code.


- if(typeof flash!= 'undefined')
    include ../mixin.jade
    mixin flash-messages(flash)


Finally routes need to be intelligent to set flash messages based on outcome of the data. This can be managed through req.flash


function findLogin(req, res,next) {
        Login.find({'email':req.body.nwemail,'password':req.body.password },function(err, profile) {
              req.session.email = req.body.email;
              if(uses.length !== 0) {
                    req.students = profile;
                     req.flash('info','Log In Successful');
                     res.redirect('/profile')
                }
               else {
                     req.flash('warning',Oops);
                     res.redirect('/login')
               }                             
                }); 
        }


Hope this helps to beautify your project. In case of queries post it over here.

 

Join the discussion

Improve the thread with your input and questions.
Carl Usmov
Consultant at IT Synergy Inc

Member Since : Dec, 2012

San Francisco California

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable networ

Take course on


Interested in node.js? We will notify you when knowledge is shared.