Search code examples
javascriptnode.jsexpressconnect-flash

Understanding req.flash()


I am still confusing about using req.flash in nodejs.

I have .catch like..

Login function

.catch(function(e){
  req.flash('errors', 'error here')
  res.redirect('/')
})

So this is I make a custom request object with property flash?

And because I'm using redirect, now the response object will send the request with custom request to the '/' route?


Solution

  • I believe you are using req-flash.

    If you use req.flash() in the controller of your '/' endpoint, you'll get the following object:

    {
      errors: 'error here',
    }
    

    You can then do whatever you want from this point.

    A common case of usage is to use this to display messages conditionaly. For instance, you can display an alert if req.flash().errors is defined.