ExpressJS 4.2和flash错误(TypeError:Object#<incomingmessage>没有方法&#39; flash&#39;)</incomingmessage>

时间:2014-07-28 16:12:16

标签: javascript node.js express passport.js

我正在使用ExpressJS 4.2和PassportJS来验证本地用户。 一切都很好,除非我尝试上升failureFlash消息。 这是我的conf,提前谢谢!

====需要app.js

var express      = require('express');
var path         = require('path');
var favicon      = require('static-favicon');
var logger       = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser   = require('body-parser');
var passport     = require('passport')
var mongoose     = require('mongoose');
var flash        = require('connect-flash');
在app.js中

==== config

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser('secret'));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));

==== / admin route(routes / admin.js)

router.post('/admin', passport.authenticate('loginAdmin',{ successRedirect: '/panel',
                                                failureRedirect: '/admin',
                                                failureFlash: true }));

====护照文件(config / passport.js)

passport.use('loginAdmin', new LocalStrategy(

  function(username, password, done) {



    modeloUsuario.findOne({ nombre: username, password: password }, function(err, user)  {



      if (err) { return done(err); }

      if (!user) {

        return done(null, false, { message: 'Incorrect username.' });
      }


      return done(null, user, {message: "invalid login"}); //<- error problem
    });
  }
));

====最后,我的package.json

{
  "name": "test",
  "version": "0.0.2",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "~4.2.0",
    "connect-flash": "latest",
    "static-favicon": "~1.0.0",
    "morgan": "~1.0.0",
    "cookie-parser": "~1.0.1",
    "body-parser": "~1.0.0",
    "debug": "~0.7.4",
    "ejs": "~0.8.5",
    "passport": "latest",
    "passport-local": "latest",
    "mongoose": "latest"
  }
}

错误:

Github/express-auth/node_modules/passport/lib/middleware/authenticate.js:111
          req.flash(type, msg);
              ^
TypeError: Object #<IncomingMessage> has no method 'flash'

1 个答案:

答案 0 :(得分:3)

您尚未在 app.js 中的中间件中初始化flash

在护照中间件之前添加app.use(flash)应解决问题。

有关详细信息,请参阅connect-flash #usage