title: Using flash message after submitting form in Nodejs
body: <p>I am just trying to show a bootstrap style alert after submitting a contact form with Node JS/Express. I am using an ejs template for my view</p>
  
  <p><strong>mailerRoutes.js</strong></p>
  
  <pre><code>const nodemailer = require('nodemailer');
  const mailer = require('express').Router();
  
  &lt;!-- more mailer config here ---&gt;
  
  function sendEmail(req, res) {
   transporter.sendMail(mailOptions, function(error, info){
    if(error){
      req.flash('msg', 'error occurred');
      res.locals.messages = req.flash();
    }else{
      req.flash('msg', 'Successfully sent email');
      res.locals.messages = req.flash();
    };
    res.redirect('/');
   });
  }
  
  module.exports = mailer;
  </code></pre>
  
  <p><strong>server.js</strong></p>
  
  <pre><code>const mailerRoutes = require('./routes/mailerRoutes');
  
  app.use(cookieParser('secret'));
  app.use(session({ 
    cookie: { maxAge: 60000 },
    secret: process.env.SESSION_SECRET || '&lt;mysecret&gt;',
    resave: false,
    saveUninitialized: false 
  }));
  app.use(flash());
  
  app.use('/changeInfo', mailerRoutes);
  
  app.get('/', function(req, res) {
    RugbyClub.count({}, (err, count) =&gt; {
    if (err) {
      res.render('index', { count: [] });
      return;
    }
      res.render('index', { count: count });
   });
  });
  </code></pre>
  
  <p><strong>index.ejs</strong></p>
  
  <pre><code>&lt;% if (locals.messages) { %&gt;
  
  &lt;script language="javascript"&gt;
    alert("&lt;%= messages.msg %&gt;");
  &lt;/script&gt;
  
  &lt;% } %&gt;
  </code></pre>
  
  <p>I'm clearly doing something wrong as nothing happens but I would like to redirect and then show the message, is that possible?</p>