【问题标题】:ECONNREFUSED error on stream is not handled by error event and try catch流上的 ECONNREFUSED 错误未由错误事件处理并尝试捕获
【发布时间】:2017-02-08 15:57:18
【问题描述】:

我正在使用 npm request 模块将传入的请求转发到另一台服务器:

app.get("/somepath", function(req, res) {
    var url = proxySetting.global.url + req.url;
    req.pipe(request(url)).pipe(res);
});

这里:proxySetting.global.url == http://localhost:4000

现在当我将这样的传入请求转发到目标服务器时,如果目标服务器(localhost:4000) 已关闭或请求在目标服务器上挂起。

会出现ECONNREFUSED或挂断错误等错误。

尝试使用下面的域模块捕获这些错误

var d = domain.create();
d.on("error", function(err) {
    console.log("Error occoured while forwarding request");
    console.log(err);
    res.status(500).send("Error occoured while forwarding request");
});
d.run(function() {
    req.pipe(request(url)).pipe(res);
});

尝试在几种组合中捕获错误事件

    var request = require("request");

module.exports = function(proxySetting) {
    return function(req, res, next) {
        var url = proxySetting.global.url + req.url;
        url = url.replace(/([^:]\/)\/+/g, "$1") // replacing multiple slashes with one
        console.log("Forwarding request to: " + url);

        function errorHandler(err) {
            console.log("Error occoured while forwarding request");
            console.log(err);
            res.status(500).send("Error occoured while forwarding request");
        }
        req.on("error", errorHandler);
        res.on("error", errorHandler);
        req.pipe(request(url).on("error",errorHandler)).pipe(res);
    };
};

但仍然向进程抛出异常并且服务器崩溃 我现在正在做的一种方法是

process.on('uncaughtException', function(err) {
  console.log("Some unhandled error occoured");
  console.log(err);
  console.log("Stopping server");
  process.exit(1);
});

但我认为 catch uncaughtException 和 handle 不是一个合适的解决方案

【问题讨论】:

  • 我不知道 nodeJS 错误处理,但请看这个,stackoverflow.com/questions/25846475/…。这是您要找的吗?
  • 这是问题的部分解决方案,根据他们要求检查连接然后将请求转发到远程服务器的解决方案。但是还有另一种情况,远程服务器启动但请求在远程服务器上挂起,我们将收到一个挂起错误,这将终止代理服务器进程。

标签: node.js express error-handling exception-handling


【解决方案1】:

看来,您没有对"errorHandler" 中的响应对象采取行动,如果您看到您的代码块(如下所示),则res 超出范围。

function errorHandler(err) {
    console.log("Error occoured while forwarding request");
    console.log(err);
    res.status(500).send("Error occoured while forwarding request"); 
} 
req.on("error", errorHandler); 
res.on("error", errorHandler); 
req.pipe(request(url).on("error", errorHandler)) .pipe(res);

但是,如果您在错误句柄中对响应对象进行操作,则不会有任何uncaughtException

我已经创建了一个解决方案,如果代理服务器宕机,它将作用于错误句柄上的响应对象,因此它不会命中 uncaughtException 事件。

这是解决方案。

var response ;

app.get('/', function(req, res){
    var url = "http://localhost:3001" + req.url;
    response = res; // I am setting res to global variable here
    req.pipe(request(url).on("error",errorHandler)).pipe(res);

});


function errorHandler(err) {
    response.status(500).send("Error occoured while forwarding request");
}

process.on('uncaughtException', function(err) {
  console.log("Some unhandled error occoured");
  console.log(err);
  console.log("Stopping server");
  process.exit(1);
});

这里uncaughtException事件不会被调用,我已经签入了repo的解决方案,你可以试一试。回购位置 - here

成功案例使用“Run1.sh”,代理服务器宕机案例使用“Run2.sh”。

【讨论】:

  • 你好Arvind,问题与res无关,超出范围,实际代码更新有问题
  • 我尝试了相同的更新代码,现在它正在工作,不知道如何,我更新了请求包可能是由于需要调查。
  • @MayankKansal 你能用你的请求包版本更新我吗,以便我尝试重现相同的版本。
  • 2.75.0是当前版本的request包,不确定之前的版本。
  • @MayankKansal,好的,没问题。
猜你喜欢
  • 2019-07-22
  • 2023-02-15
  • 2019-01-07
  • 2020-03-16
  • 2019-08-30
  • 2013-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多