title: Session flash messages only showing after a separate refresh and not on POST in express
body: <p>Started with Express a couple weeks ago and finally hit my first roadblock. I'm trying to figure out how to send a flash message to my view (layout.jade). When I'm validating a user signup and it fails validation (using express-validator), I want to write to the session flash message and then show it on the page.</p>
  
  <p>What I'm doing now only works if I refresh AFTER POSTing (e.g. in controllers/index.js), and I also need to figure out how to only make this flash message show once:</p>
  
  <p>/app.js</p>
  
  <pre><code>/*
   * Configuration
   */
  app.configure(function() {
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(expressValidator);
    app.use(express.cookieParser());
    app.use(express.bodyParser({ uploadDir : './' }));
    app.use(express.methodOverride());
    app.use(viewHelpers());
    app.use(function(req, res, next){
      res.locals.messages = req.session.messages || {};
      delete req.session.messages;
      next();
    });
    app.use(express.session({
      secret: config.secret,
      cookie: {
        maxAge: 365 * 24 * 60 * 60 * 1000
      },
      store: new MongoStore(config.db)
    }));
    app.use(express.static(__dirname + '/public'));
    app.use(app.router);
  });
  </code></pre>
  
  <p>/views/layout.jade:</p>
  
  <pre><code>div.alert.alert-error
    - if (typeof messages !== "undefined")
      !=partial('session_flash', { messages : messages })
  </code></pre>
  
  <p>/views/partials/session_flash.jade:</p>
  
  <pre><code>div.alert.alert-error
    each message in messages
      =message
  </code></pre>
  
  <p>/controllers/index.js</p>
  
  <pre><code>req.assert(['user', 'username'], 'Please enter a valid username').len(3, 23);
  req.assert(['user', 'email'], 'Please enter a valid email').isEmail();
  req.assert(['user', 'password'], 'Please enter a valid password').len(3, 23);
  req.session.messages = 'Please fix the errors below';
      errors = req.validationErrors();
  
      if (errors) {
        console.log(errors);
        res.render('join', {
          title : 'Join -',
          errors: errors,
          params: params
        });
        return;
      }
  </code></pre>
  
  <p>Is it only showing up after I refresh (after a POST) because I'm rendering the join template from within <code>/controllers/index.js</code> and not <code>/app.js</code>? Also, should I be doing something like <code>delete req.session.messages</code> within my configuration? I couldn't find a complete example of this anywhere and the ones I've found were outdated.</p>