使用 Redis 作为会话存储的 NodeJs 抛出异常
我正在尝试使用 NodeJs 和 Session。我跟踪了许多网站的代码。我想提到的最简单的一个是 <一href="https://medium.com/swlh/session-management-in-nodejs-using-redis-as-session-store-64186112aa9#:%7E:text =%20session%20ID%20是%20发送%20回%20到%20,%20特定%20session%20id%20在%20%20Redis%20存储中。” rel="nofollow noreferrer">使用 Redis 进行会话管理的示例 NodeJS 代码
论坛的其余部分都发现了类似的内容。我面临的问题是,当我不使用 Redis 作为会话存储时,一切正常。下面是从 redis 会话切换到内存会话的代码更改。
app.use(session({
secret: 'secret$%^134',
resave: false,
saveUninitialized: false,
cookie: {
secure: false, // if true only transmit cookie over https
httpOnly: false, // if true prevent client side JS from reading the cookie
maxAge: 1000 * 60 * 10 // session max age in miliseconds
}
}))
但当我将 Redis 存储放回到示例中时,我开始收到异常客户端已关闭。启用 Redis 会话存储的代码是
const RedisStore = connectRedis(session)
//Configure redis client
const redisClient = redis.createClient({
host: 'localhost',
port: 6379
})
redisClient.on('error', function (err) {
console.log('Could not establish a connection with redis. ' + err);
});
redisClient.on('connect', function (err) {
console.log('Connected to redis successfully');
});
//Configure session middleware
app.use(session({
store: new RedisStore({ client: redisClient }),
secret: 'secret$%^134',
resave: false,
saveUninitialized: false,
cookie: {
secure: false, // if true only transmit cookie over https
httpOnly: false, // if true prevent client side JS from reading the cookie
maxAge: 1000 * 60 * 10 // session max age in miliseconds
}
}));
I was experimenting with NodeJs with Session. I followed code from many sites. Simplest one I would like to mentioned is
Sample NodeJS Code for session management with Redis
Rest of the forums found similar to it. Issue I am facing is when I am not using the redis as session store everything work fine. Below changes in code for swithcing from redis session to In memory session.
app.use(session({
secret: 'secret$%^134',
resave: false,
saveUninitialized: false,
cookie: {
secure: false, // if true only transmit cookie over https
httpOnly: false, // if true prevent client side JS from reading the cookie
maxAge: 1000 * 60 * 10 // session max age in miliseconds
}
}))
But as I put the Redis store back in sample, I start getting exception Client is closed. Code for enabling the Redis session store is
const RedisStore = connectRedis(session)
//Configure redis client
const redisClient = redis.createClient({
host: 'localhost',
port: 6379
})
redisClient.on('error', function (err) {
console.log('Could not establish a connection with redis. ' + err);
});
redisClient.on('connect', function (err) {
console.log('Connected to redis successfully');
});
//Configure session middleware
app.use(session({
store: new RedisStore({ client: redisClient }),
secret: 'secret$%^134',
resave: false,
saveUninitialized: false,
cookie: {
secure: false, // if true only transmit cookie over https
httpOnly: false, // if true prevent client side JS from reading the cookie
maxAge: 1000 * 60 * 10 // session max age in miliseconds
}
}));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过大量阅读后,我在 npmjs 网站上得到了答案。
redis-connect 包的 npmjs 站点
在版本 4 之后,需要进行少量代码更改。需要启用“传统模式”,并且需要在代码中进行明确的“连接”调用。
更改后的工作代码如下。添加了更改的注释。
After lots of reading, I got the answer at npmjs site.
npmjs site for redis-connect package
After version 4, small code change is requried. "legacy-mode" needs to be enabled and explicite 'connect' call need to made in code.
The changed and working code is as below. Added the comments for the changes.