我有一个使用redis云在Heroku中存储会话的快速应用程序。几个月前它运作良好,但我刚刚重新访问它,它已不再有效。似乎没有会话,并抛出错误TypeError: Cannot read property 'user' of undefined
,违规行代码是
if (!req.session.user) {
...
...
}
也许我的配置不正确
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var redis = require('redis-url').connect(config.redis_url);
var sessionMiddleware = session({
store: new RedisStore({
host: config.redis_host,
port: config.redis_port
}),
secret: 'Its a secret.',
cookie: { secure: true },
saveUninitialized: true,
resave: true
});
相应的配置文件是
module.exports = {
mongo_url: process.env.MONGOLAB_URI || 'mongodb://127.0.0.1:27017/psa',
redis_url: process.env.REDISCLOUD_URL || 'redis://localhost:6379',
redis_host: taken from process.env.REDISCLOUD_URL,
redis_port: taken from process.env.REDISCLOUD_URL,
port: process.env.PORT || 5000
};
当我按如下方式定义会话中间件时,本地forman start web
var sessionMiddleware = session({
secret: 'VkWdLXauq6ya',
saveUninitialized: true,
resave: true,
store: new RedisStore({
client: redis
}),
cookie: {
path: '/',
maxAge: 3600000
},
name: 'sessionCookie'
});
答案 0 :(得分:1)
答案 1 :(得分:0)
您是如何使用创建的会话中间件的?例如:
var sessionMiddleware = session({
创建一个可以在快速堆栈中使用的函数。但是,在你真正使用它之前它什么也没做,比如:
var app = express();
app.use(sessionMiddleware);