使用 Redis 作为会话存储的 NodeJs 抛出异常

发布于 2025-01-10 10:34:37 字数 1646 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-01-17 10:34:37

经过大量阅读后,我在 npmjs 网站上得到了答案。
redis-connect 包的 npmjs 站点

在版本 4 之后,需要进行少量代码更改。需要启用“传统模式”,并且需要在代码中进行明确的“连接”调用。

更改后的工作代码如下。添加了更改的注释。

const RedisStore = connectRedis(session)
//Configure redis client
const redisClient = redis.createClient({
    host: 'localhost',
    port: 6379,
    legacyMode:true //********New Addition - set legacy mode to true.*******
})
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');
});
redisClient.connect(); //******** New Addition - Explicite connect call *********
//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
    }
}));

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.

const RedisStore = connectRedis(session)
//Configure redis client
const redisClient = redis.createClient({
    host: 'localhost',
    port: 6379,
    legacyMode:true //********New Addition - set legacy mode to true.*******
})
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');
});
redisClient.connect(); //******** New Addition - Explicite connect call *********
//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
    }
}));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文