nodejs在redis失败时终止

twb*_*twb 4 redis node.js express socket.io

我正在使用Node.js,Express,Redis和Socket.io.当Redis关闭时,Node.js将终止.

我怎样才能防止这种情况,可能是代码重新连接的某个地方?

输出:

info - socket.io started
Express server listening on port 3000
Run Code Online (Sandbox Code Playgroud)

错误:

events.js:72
        throw er; // Unhandled 'error' event
              ^ Error: Redis connection to 127.0.0.1:6380 failed - connect ECONNREFUSED
    at RedisClient.on_error (...../node_modules/redis/index.js:151:24)
    at Socket.<anonymous> (...../node_modules/redis/index.js:86:14)
    at Socket.EventEmitter.emit (events.js:95:17)
    at net.js:426:14
    at process._tickCallback (node.js:415:13)
Run Code Online (Sandbox Code Playgroud)

Tan*_*pal 10

您似乎没有redis客户端实例的错误处理程序.你能从你的代码中提取一些摘录吗?也许我们可以通过这种方式帮助你.只是一个错误消息少一点.

如果我不得不猜测那么我会说你没有错误处理程序......

请参见:https://github.com/mranney/node_redis#usage

你有错误的回调吗?

例:

var redis = require("redis"),
client = redis.createClient();


// This is required so your error doesn't bubble
// upwards and kills your instance

client.on("error", function (err) {
    console.log("Error " + err);
});
Run Code Online (Sandbox Code Playgroud)

node_redis客户端会自动重新连接,因此您无需处理,尽管您可以配置max_attempts和其他选项.请看这里:https://github.com/mranney/node_redis#rediscreateclientport-host-options

我希望我能提供一些有用的信息