nodejs在redis失败时终止

时间:2013-09-01 12:45:16

标签: node.js express redis socket.io

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

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

输出:

info - socket.io started
Express server listening on port 3000

错误:

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)

1 个答案:

答案 0 :(得分:9)

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

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

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