gpt4 book ai didi

node.js - Redis Cloud 和 connect-redis : the client is closed

转载 作者:行者123 更新时间:2023-12-05 01:26:12 26 4
gpt4 key购买 nike

我目前使用Express建立一个网站,并希望使用redis云数据库在session中保存userID。 redisClient 是在 redisClient.js 中创建的,之后我将它传递给 app.js session 中的 redisStore。这是代码:

redisCLient.js

const redis = require("redis");
let redisClient = redis.createClient({
host: process.env.REDIS_HOSTNAME,
port: parseInt(process.env.REDIS_PORT),
password: process.env.REDIS_PASSWORD
});

redisClient.on('error', function(err) {
console.log('*Redis Client Error: ' + err.message);
});
redisClient.on('connect', function(){
console.log('Connected to redis instance');
});

(async () => {
await redisClient.auth(process.env.REDIS_PASSWORD)
.catch(err => {console.log('Redis auth error: ' + err.message)});
await redisClient.connect()
.catch(err => {console.log('Redis connect error: ' + err.message)});
})();

module.exports = redisClient;

app.js

const session = require("express-session");
const redisStore = require('connect-redis')(session);
const redisClient = require('./session-store/redisClient');

...

app.use(cookieParser());
app.use(session({
store: new redisStore({client: redisClient, ttl: 3600 * 24 * 30}),
saveUninitialized: false,
secret: process.env.SESSION_SECRET,
resave: false
}));

问题是:启动服务器后,我在控制台中收到错误消息,如下所示:

Redis auth error: The client is closed
*Redis Client Error: connect ECONNREFUSED 127.0.0.1:6379
*Redis Client Error: connect ECONNREFUSED 127.0.0.1:6379
*Redis Client Error: connect ECONNREFUSED 127.0.0.1:6379
*Redis Client Error: connect ECONNREFUSED 127.0.0.1:6379
...

我用了这个guide设置 redis 云并分配 dotenv 变量(主机、端口和密码)。我已经调试并且 dotenv 工作正常并且主机、端口和密码变量正确。

但问题依然存在。我仍然收到 The client is closedconnect ECONNREFUSED 127.0.0.1:6379 错误,如上面的控制台日志所示。我该如何解决这个问题?

最佳答案

被困在同一个问题上并找到了一些运气。使用了“ioredis”模块而不是无缝工作的 redis。

const redis = require('ioredis');
const redisClient = redis.createClient({host:'your host address',port:your port,username:'',password:''});

redisClient.on('connect',() => {
console.log('connected to redis successfully!');
})

redisClient.on('error',(error) => {
console.log('Redis connection error :', error);
})

module.exports = redisClient;

关于node.js - Redis Cloud 和 connect-redis : the client is closed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70402383/

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