2017-02-17 175 views
0

當我檢查我的redis服務器狀態時,它表示redis服務器正在運行,但當我登錄時出現此錯誤 RedisClient .on('error',function(err){console.log('Redis error:'+ err); // Redis error:Error:Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379 }); 當我嘗試控制req.session時,它返回undefined。關於這個問題的任何線索? 請幫我我是新來的這個。謝謝Redis錯誤:錯誤:Redis連接到127.0.0.1:6379失敗 - 連接ECONNREFUSED 127.0.0.1:6379

繼承人我的設置:

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); 
 
});

回答

0

你在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"); 
}); 
+0

嗨,謝謝。我試過你提供的代碼,但仍然遇到同樣的問題:( – fyeah

+0

當我通過redis-cli ping檢查redis服務器時,它顯示: 無法連接到Redis 127.0.0.1:6379:連接拒絕 – fyeah

+0

Hi @fyeah ,你能告訴我你的操作系統環境,所以我可以進一步幫助你 –

相關問題