当前位置: 首页 > 知识库问答 >
问题:

Redis错误:错误:Redis连接到127.0.0.1:6379失败-连接econn拒绝127.0.0.1:6379

咸利
2023-03-14
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);
});

共有1个答案

莘康裕
2023-03-14

您在Redisstore选项中一起提供了互斥参数。您要么传递主机和端口,要么只传递现有的redis客户端。

引用connect-redis文档-

需要Redis客户端。现有客户端可以使用客户端参数直接传递,也可以使用主机、端口或套接字参数为您创建

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");
});
 类似资料: