title: Using express-flash or connect-flash is resulting in blank messages object
body: <p>I'm trying to make a NodeJS web server using express, and came across a weird bug I can't figure out.</p>
  <p>I am quite familiar with Express &amp; EJS, and this time I wanted to use connect-flash (or express-flash) to display flash messages.
  I read a bit about those 2 and watched a few tutorials, but cannot understand why it is not working.
  The results are pretty much the same for connect-flash and express-flash, and they are as following:</p>
  <p>I am importing express, session and then flash, initiate the view engine, the session and then the flash:</p>
  <pre><code>const express        = require(&quot;express&quot;);
  
  const session        = require(&quot;express-session&quot;);
  const flash          = require(&quot;express-flash&quot;);
  
  app.set(&quot;view engine&quot;, &quot;ejs&quot;);
  
  app.use(session({
      secret: process.env.SESSION_SECRET,
      resave: true,
      saveUninitialized: true,
      cookie: { secure: true }
  }));
  
  app.use(flash());
  </code></pre>
  <p>I then do some authentication stuff. For example, I use passport to authenticate when a user registers:</p>
  <pre><code>router.post(&quot;/register&quot;, (req, res) =&gt; {
      const { name, email, password, confirmpassword } = req.body;
      let errors = [];
  
      if(!name || !email || !password || !confirmpassword)
          errors.push({ message: &quot;Please fill in all fields!&quot; });
      
      if(password !== confirmpassword) 
          errors.push({ message: &quot;Passwords do not match!&quot; });
  
      if(password.length &lt; 6)
          errors.push({ message: &quot;Password should be at least 6 characters long!&quot; });
  
      if(errors.length &gt; 0) {
          res.render(&quot;register&quot;, { errors, name, email });
      } else {
          User.findOne({ email: email })
          .then(user =&gt; {
              if(user) {
                  errors.push({ message: &quot;A user with this email address already exists!&quot; });
                  res.render(&quot;register&quot;, { errors, name, email });
              } else {
                  const newUser = new User({
                      name,
                      email,
                      password
                  });
  
                  bcrypt.genSalt(10, (error, salt) =&gt; {
                      if(error)
                          throw error;
                      
                      bcrypt.hash(newUser.password, salt, (error, hash) =&gt; {
                          if(error)
                              throw error;
                          
                          newUser.password = hash;
                          newUser.save()
                              .then(user =&gt; {
                                  req.flash(&quot;success_message&quot;, &quot;You have successfully registered!&quot;);
                                  res.redirect(&quot;/login&quot;);
                              })
                              .catch(error =&gt; console.log(error));
                      })
                  })
              }
          });
      }
  });
  </code></pre>
  <p>and this is the part I can't seem to understand.
  As you can see, I pass a flash message (success_message) when a new user is created. The redirect is working perfectly and in general, the whole server is working great, but the flash messages are just blank.
  It happens in other instances as well, for example when logging out:</p>
  <pre><code>router.get(&quot;/logout&quot;, (req, res) =&gt; {
      req.logout();
      req.flash(&quot;success_message&quot;, &quot;You logged out successfully!&quot;);
      res.redirect(&quot;/login&quot;);
  });
  </code></pre>
  <p>Or when loading a page before logging in:</p>
  <pre><code>    ensureAuthenticated: (req, res, next) =&gt; {
          if(req.isAuthenticated())
              return next();
  
          req.flash(&quot;error_message&quot;, &quot;Please log in to view that resource&quot;);
          res.redirect(&quot;/login&quot;);
      },
  </code></pre>
  <p>Or even in the passport strategy itself:</p>
  <pre><code>new localStrategy({ usernameField: 'email', passReqToCallback: true }, (req, email, password, done) =&gt; {
              User.findOne({ email: email })
                  .then(user =&gt; {
                      if(!user)
                          return done(null, false, req.flash(&quot;error_message&quot;, &quot;The email and password you entered did not match any of our records. Please double-check and try again.&quot;));
  
                      bcrypt.compare(password, user.password, (error, match) =&gt; {
                          if(error)
                              throw(error);
  
                          if(match)
                              return done(null, user);
  
                          return done(null, false, req.flash(&quot;error_message&quot;, &quot;The email and password you entered did not match any of our records. Please double-check and try again.&quot;));
                      });
                  })
                  .catch(error =&gt; console.log(error));
          })
      );
  </code></pre>
  <p>I try to load the flash messages through a partial page, called messages.ejs:</p>
  <pre><code>&lt;% console.log(locals) %&gt;
  &lt;% console.log(messages) %&gt;
  
  &lt;% if(messages.success_message) { %&gt;
      &lt;div class=&quot;alert alert-success alert-dismissible fade show&quot; role=&quot;alert&quot;&gt;
          &lt;%= messages.success_message %&gt;
          &lt;button type=&quot;button&quot; class=&quot;close&quot; data-dismiss=&quot;alert&quot; aira-label=&quot;Close&quot;&gt;
              &lt;span aria-hidden=&quot;true&quot;&gt;&amp;times;&lt;/span&gt;
          &lt;/button&gt;
      &lt;/div&gt;
  &lt;% } %&gt;
  
  
  &lt;% if(messages.error_message) { %&gt;
      &lt;div class=&quot;alert alert-danger alert-dismissible fade show&quot; role=&quot;alert&quot;&gt;
          &lt;%= messages.error_message %&gt;
          &lt;button type=&quot;button&quot; class=&quot;close&quot; data-dismiss=&quot;alert&quot; aira-label=&quot;Close&quot;&gt;
              &lt;span aria-hidden=&quot;true&quot;&gt;&amp;times;&lt;/span&gt;
          &lt;/button&gt;
      &lt;/div&gt;
  &lt;% } %&gt;
  </code></pre>
  <p>but the first 2 console.log simply returns the locals object, in which you can clearly see the messages are empty.</p>
  <pre><code>{
    settings: {
      'x-powered-by': true,
      etag: 'weak',
      'etag fn': [Function: generateETag],
      env: 'development',
      'query parser': 'extended',
      'query parser fn': [Function: parseExtendedQueryString],
      'subdomain offset': 2,
      'trust proxy': false,
      'trust proxy fn': [Function: trustNone],
      view: [Function: View],
      views: 'D:\\Workspace JavaScript\\Passager\\views',
      'jsonp callback name': 'callback',
      'view engine': 'ejs'
    },
    messages: {},
    contentFor: [Function: contentFor],
    _locals: [Object: null prototype] { messages: {} },
    cache: false
  }
  </code></pre>
  <p>I don't log the flash messages anywhere and I don't do anything to otherwise &quot;delete&quot; them. I even tried passing the manually throw the router.get requests:</p>
  <pre><code>router.get(&quot;/login&quot;, forwardAuthenticated, (req, res) =&gt; res.render(&quot;login&quot;, {message: req.flash('success_message')}));
  router.get(&quot;/register&quot;, forwardAuthenticated, (req, res) =&gt; res.render(&quot;register&quot;, {message: req.flash('success_message')}));
  </code></pre>
  <p>Yet, can't seem to understand why they are not showing.</p>
  <p>Does anyone have any ideas? :)</p>