使用 Redis 作为会话存储的 NodeJs 抛出异常
NodeJs with Redis as session store throws exception
我正在尝试使用带有 Session 的 NodeJs。我关注了许多网站的代码。我想提到的最简单的一个是
Sample NodeJS Code for session management with Redis
其余论坛发现类似。我面临的问题是当我不使用 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
}
}));
经过大量阅读,我在 npmjs 站点得到了答案。
npmjs site for redis-connect package
在版本 4 之后,需要对代码进行少量更改。 “legacy-mode”需要启用,并且需要在代码中进行显式 'connect' 调用。
更改后的工作代码如下。添加了更改的注释。
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
}
}));
我正在尝试使用带有 Session 的 NodeJs。我关注了许多网站的代码。我想提到的最简单的一个是 Sample NodeJS Code for session management with Redis
其余论坛发现类似。我面临的问题是当我不使用 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
}
}));
经过大量阅读,我在 npmjs 站点得到了答案。 npmjs site for redis-connect package
在版本 4 之后,需要对代码进行少量更改。 “legacy-mode”需要启用,并且需要在代码中进行显式 'connect' 调用。
更改后的工作代码如下。添加了更改的注释。
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
}
}));