Redis error: Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379

Redis error: Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379

当我检查我的 redis-server 状态时,它说 redis 服务器是 运行 但我在登录时遇到了这个错误 RedisClient.on('error', 函数(错误) { console.log('Redis error: ' + err);// Redis错误:Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379 }); 当我尝试控制 req.session 时,它 returns 未定义。关于这个问题的任何线索? 请帮助我,我是新来的。谢谢

这是我的设置:

var express = require('express');
var redis = require("redis");
var session = require('express-session');
var redisStore = require('connect-redis')(session);
var parser = require('body-parser');
var RedisClient= redis.createClient();

app.use(session({
secret: 'myseceret',
store: new redisStore({host: '127.0.0.1', port: 6379, client: client,ttl : 260}),
saveUninitialized: false,
resave: false,
cookie: { secure: true }
}));
client.on('error', function(err) {
console.log('Redis error: ' + err);
});

您在redisStore 的选项中同时提供了互斥参数。您要么传递主机和端口,要么传递现有的 Redis 客户端。

引用 connect-redis 文档 -

A Redis client is required. An existing client can be passed directly using the client param or created for you using the host, port, or socket params

还有一些参考错误,我已在此代码段中更正。

var express = require('express');
var redis = require("redis");
var session = require('express-session');
var redisStore = require('connect-redis')(session);
var parser = require('body-parser');
var RedisClient= redis.createClient();

var app = express();

app.use(session({
    secret: 'mysecret',
    store: new redisStore({client: RedisClient,ttl : 260}),
    saveUninitialized: false,
    resave: false,
    cookie: { secure: true }
}));

RedisClient.on('error', function(err) {
    console.log('Redis error: ' + err);
});

RedisClient.on("ready",function () {
    console.log("Redis is ready");
});