Snorflake Snorflake - 1 year ago 108
Node.js Question

How can I delete flash messages once a page has loaded using express?

So I was using express.js under node.js and came across this question. I have a login page where if you get a password/username wrong it will flash a message (Basic stuff using passport). But if you keep trying, the other error messages will stack (Only 1 of each, but they're still there after they're not relevant).

Is there a way i can delete the flash messages once they've been displayed? I'm using this flash.

Here's what I've tried so far:

router.get("/login", function(req, res){
res.locals.flash = [];

and my post req"/login", passport.authenticate(
successRedirect: "/",
failureRedirect: "/login",
failureFlash: true

Answer Source

I suggest you use the connect-flash package, with this one you can do smtg like this :'/login', function(req, res){
  // if credentials are wrong
  req.flash('msg', 'Try again !')

app.get('/login', function(req, res){
  res.render('login', { message : req.flash('msg') });

In the views, you just need to check for message existence and output it , with ejs it would be smtg like this :

<% if(message){ %> 
  <%= message %>
<% } %>


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download