gpt4 book ai didi

javascript - ExpressJS 4.2 和闪存错误(TypeError : Object # has no method 'flash' )

转载 作者:搜寻专家 更新时间:2023-11-01 00:44:07 26 4
gpt4 key购买 nike

我正在使用 ExpressJS 4.2 和 PassportJS 对本地用户进行身份验证。一切都很好,除非我尝试发出失败的 Flash 消息。这是我的 session ,提前致谢!

==== 需要在 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 中的配置

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 路由 (routes/admin.js)

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

==== Passport 文件 (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'

最佳答案

您还没有在 app.js 的中间件中初始化 flash

在 passport 中间件之前添加 app.use(flash) 应该可以解决问题。

参见 connect-flash #usage了解更多信息。

关于javascript - ExpressJS 4.2 和闪存错误(TypeError : Object #<IncomingMessage> has no method 'flash' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24999493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com