gpt4 book ai didi

node.js - connect-redis 和 express-session 导致 req.session undefined

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

我正在尝试让 express-session 将 session 存储在 Redis 中,但它似乎并不想保存。当我恢复到默认 session 存储时,它可以完美运行。 Redis 守护程序托管在具有默认配置的 Vagrant VM 上,应用程序能够连接到它,尽管它不想保存 session 。

这是我的代码:

var express = require('express');
var glob = require('glob');

var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var compress = require('compression');
var methodOverride = require('method-override');
var session = require('express-session');
var passport = require('passport');
var redis = require('redis');
var RedisStore = require('connect-redis')(session);
var auth = require('./passport');
var flash = require('connect-flash');

module.exports = function(app, config) {
app.set('views', config.root + '/app/views');
app.set('view engine', 'jade');

// app.use(favicon(config.root + '/public/img/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cookieParser());
app.use(session({
store: new RedisStore({
host: config.redis.host,
port: config.redis.port
}),
secret: config.secret,
saveUninitialized: true,
resave: false
}));
/*app.use(session({
secret: config.secret,
saveUninitialized: true,
resave: true
}));*/
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(compress());
app.use(express.static(config.root + '/public'));
app.use(methodOverride());

var controllers = glob.sync(config.root + '/app/controllers/*.js');
controllers.forEach(function (controller) {
require(controller)(app);
});

app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

if(app.get('env') === 'development'){
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err,
title: 'error'
});
});
}

app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {},
title: 'error'
});
});

};

如您所见,我已注释掉默认的注释,如果我删除这些注释并注释掉 redis 存储,它就会正常工作。为什么会发生这种情况的任何线索?我没有收到任何错误。

最佳答案

您可能有一些连接错误,但 connect-redis 不会将它们输出到控制台(请参阅 connect-redis source code )。要查看它们,您可以创建一个单独的模块来创建客户端实例并将其传递给 RedisStore 构造函数:

// redisClient.js
var redis = require('redis');

var redisClient = redis.createClient('localhost', 6379); // replace with your config

redisClient.on('error', function(err) {
console.log('Redis error: ' + err);
});

module.exports = redisClient;

Redis 客户端还会发出其他可能有助于调试的事件 - 参见 node-redis docs

// your code
var redisClient = require('./redisClient.js`);

(...)

app.use(session({
store: new RedisStore({
client: redisClient
}),
secret: config.secret,
saveUninitialized: true,
resave: false
}));

关于node.js - connect-redis 和 express-session 导致 req.session undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27693127/

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