2016-09-30 57 views
2

我使用NPM请求模块转发传入的请求到另一台服务器为:投产ECONNREFUSED错误不是由错误事件处理,并尝试捕捉

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

但我认为抓住uncaughtException和处理不是一个合适的解决方案

+0

我不知道nodeJS错误处理,但请看看这个,http://stackoverflow.com/questions/25846475/node-js-handling-tcp-socket-error-econnrefused。你在找这个吗? –

+0

这是问题的部分解决方案,按照他们要求检查连接并将请求转发到远程服务器的解决方案。但是当远程服务器启动但是请求挂在远程服务器上时会出现另一种情况,并且我们将得到一个挂起错误,它将终止代理服务器进程。 –

回答

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事件将不会被调用,我已经解决了回购检查,你可以试试看。回购地点 - here

使用“Run1.sh”作为成功案例,代理服务器停止使用“Run2.sh”。

+0

你好Arvind,问题是不相关的res是超出范围,实际代码更新问题 –

+0

我试过相同的更新代码,它现在正在工作,不知道如何,我更新了请求包可能是由于需要调查。 –

+0

@MayankKansal你可以用你的请求包版本更新我,这样我就可以重现同样的内容。 –