使用带有护照js的节点js,处理程序栏模板上不显示Flash消息

时间:2017-03-20 09:32:01

标签: node.js connect-flash

我正在尝试使用connect-flash包在handlebar模板中显示消息,但是当我在登录页面中使用{{loginMessage}}时,消息未显示及其打印计数。但不显示消息。  这是我的代码

file..routes / index.js

 var express =  require('express');
 var router =   express.Router();
 var mongoose = require('mongoose');
 var flash =    require('connect-flash');
 const m_db =   require('.././config/mongo_connection');
 var Site =     require('.././models/site');
 var User = require('../models/user');
 var bcrypt   = require('bcrypt-nodejs');
 var passport = require('passport');
 var LocalStrategy = require('passport-local').Strategy;
 require('.././config/passport')(passport);
 //routes start here
 router.get('/login', function(req, res, next) {    
 res.render('passports/login.hbs', { message:   req.flash('loginMessage','failed login') });

})

我的app.js

  app.use(require('express-session')({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: false
}));
  app.use(flash());
 //
  app.use(passport.initialize());
  app.use(passport.session());
  app.use(express.static(path.join(__dirname, 'public')));
 // passport config

 app.use(logger('dev'));
 app.use(bodyParser.json());
 app.use(bodyParser.urlencoded({ extended: false }));
 app.use(cookieParser());
 app.use(express.static(path.join(__dirname, 'public')));

 app.use('/', index);
 app.use('/users', users);
 app.use('/api', api);

 // catch 404 and forward to error handler
  app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
 });

 // error handler
  app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
 });
  module.exports = app;

请帮忙! 谢谢!

1 个答案:

答案 0 :(得分:0)

router.get('/login', function(req, res, next) {           

  res.render('passports/login.hbs', { message: req.flash('loginMessage') }); 
     .
     .

我想说的是:

req.flash('loginMessage','failed login')  

用于将Flash变量名称loginMessage设置为failed login

req.flash('loginMessage')

这是您以后阅读闪光灯的时间。上面的代码返回loginMessage的闪存消息数组(如果设置了多个闪存)。