node.js - nodejs 在 redis 失败时终止

标签 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)

最佳答案

您的 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

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

关于node.js - nodejs 在 redis 失败时终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18558558/

相关文章:

node.js - Express.js错误: Can't set headers after they are sent

java - 每个 servlet 一个 Lettuce Redis 连接?

python - NGINX/Gunicorn/Django-RQ : How to log each rq-worker to a different file

javascript - Node.JS - 在执行其余代码之前等待函数完成

javascript - 自定义 NodeJS Grunt 命令

node.js - 如何在 Express 中返回错误消息对象给客户端?

redis - 调用使用redis-rs创建redis连接的函数时如何防止生命周期问题

node.js - 如何覆盖 Mongoose 插件的功能?

javascript - Babel 没有替换箭头函数

javascript - morgan 记录器 + Express.js : writing file AND showing in console