简体   繁体   English

req.flash不是功能-初始化顺序

[英]req.flash is not a function - Initialization order

I am using node.js with Express 4 . 我在Express 4中使用node.js。 I have problem with connect-flash initialization. 我有连接闪存初始化问题。 I looked at many questions with different answers and I still can't solve my problem. 我看了很多不同答案的问题,但仍然无法解决问题。 I tried many combinations of app.use() order and still getting TypeError req.flash is not a function 我尝试了app.use()命令的许多组合,但仍然收到TypeError req.flash is not a function

Here is my current initialization order: 这是我当前的初始化顺序:

var app = express();
var index = require('./routes/index');
var users = require('./routes/users');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(sassMiddleware({
    src: __dirname + '/public/sass',
    dest: path.join(__dirname, 'public/css'),
    debug: true,
    outputStyle: 'compressed',
    prefix:  '/css'
}));
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
  secret: 'secret',
  saveUninitialized: true,
  resave: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(expressValidator({
  errorFormatter: function(param, msg, value) {
      var namespace = param.split('.')
      , root    = namespace.shift()
      , formParam = root;

    while(namespace.length) {
      formParam += '[' + namespace.shift() + ']';
    }
    return {
      param : formParam,
      msg   : msg,
      value : value
    };
  }
}));
app.use(i18n({
  translationsPath: path.join(__dirname, './locales'), 
  siteLangs: ["es","en"]
}));
app.use(function (res,req,next) {
    res.locals.success_msg = req.flash('success_msg');
    res.locals.error_msg = req.flash('error_msg');
    res.locals.error = req.flash('error');
    next();
});

app.use('/', index);
app.use('/users', users);
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});
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');
});

Thank you for your help. 谢谢您的帮助。

app.use(function (res,req,next) {
    //...
})

Your req and res parameters in this function is in the wrong order. 您在此函数中的req和res参数顺序错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM