Express node.js connect闪存消息未显示

Express node.js connect闪存消息未显示,express,node.js-connect,Express,Node.js Connect,我试图为我试图构建的登录页面实现一个非常基本的错误消息。post请求的代码如下所示 app.post('/login',function(req,res){ User.findOne({username:req.body.user.username},function(err,info){ if(err){ req.flash('error','There was an error on our end. Sorry about that.'); res.redirect

我试图为我试图构建的登录页面实现一个非常基本的错误消息。post请求的代码如下所示

app.post('/login',function(req,res){
  User.findOne({username:req.body.user.username},function(err,info){
  if(err){
    req.flash('error','There was an error on our end. Sorry about that.');
    res.redirect('/');
  }

  if(info==null){
    req.flash('error', 'Incorrect Username. Please try again, or make an account');
    res.redirect('/login');
  }
  else{
    if(req.body.user.password==info.password){
      res.redirect('/users/' + info.username);
    }
    else{
      req.flash('error', 'Incorrect Password');
      res.redirect('/login');
    }
  }
});
});
当我触发空案例时,我从未看到错误消息,尽管重定向确实发生了。 我的路线获取方法如下:

app.get('/login',function(req,res){
   res.render('login',{
     title: 'Login'
   });
});

您需要将flash消息传递到模板,然后自己在模板中呈现它们:

app.get('/login',function(req,res){
   res.render('login',{
     title  : 'Login',
     errors : req.flash('error')
   });
});
如果使用EJS模板(例如),请检查
errors
是否包含值,并显示该值:

<% if (errors) { %>
  <p class="error"><%= errors %></p>
<% } %>