我在nodejs上编写了简单的代理,它看起来像
var request = require( 'request' );
app.all( '/proxy/*', function( req, res ){
req.pipe( request({
url: config.backendUrl + req.params[0],
qs: req.query,
method: req.method
})).pipe( res );
});
如果远程主机可用,它可以正常工作,但如果远程主机不可用,则整个节点服务器会因未处理的异常而崩溃
stream.js:94
throw er; // Unhandled stream error in pipe.
^
Error: connect ECONNREFUSED
at errnoException (net.js:901:11)
at Object.afterConnect [as oncomplete] (net.js:892:19)
我该如何处理这类错误?
答案 0 :(得分:30)
查看文档(https://github.com/mikeal/request),您应该可以按照以下方式执行操作:
您可以根据请求使用可选的回调参数,例如:
app.all( '/proxy/*', function( req, res ){
req.pipe( request({
url: config.backendUrl + req.params[0],
qs: req.query,
method: req.method
}, function(error, response, body){
if (error.code === 'ECONNREFUSED'){
console.error('Refused connection');
} else {
throw error;
}
})).pipe( res );
});
或者,您可以使用以下内容捕获未捕获的异常:
process.on('uncaughtException', function(err){
console.error('uncaughtException: ' + err.message);
console.error(err.stack);
process.exit(1); // exit with error
});
答案 1 :(得分:2)
如果您发现ECONNREFUSED的未捕获异常,请确保重新启动您的进程。我在测试中看到,如果忽略异常并且只是尝试重新连接,套接字就会变得不稳定。
这里有一个很棒的概述:http://shapeshed.com/uncaught-exceptions-in-node/
我最终使用了"永远"使用以下代码重新启动节点进程的工具:
process.on('uncaughtException', function(err){
//Is this our connection refused exception?
if( err.message.indexOf("ECONNREFUSED") > -1 )
{
//Safer to shut down instead of ignoring
//See: http://shapeshed.com/uncaught-exceptions-in-node/
console.error("Waiting for CLI connection to come up. Restarting in 2 second...");
setTimeout(shutdownProcess, 2000);
}
else
{
//This is some other exception..
console.error('uncaughtException: ' + err.message);
console.error(err.stack);
shutdownProcess();
}
});
//Desc: Restarts the process. Since forever is managing this process it's safe to shut down
// it will be restarted. If we ignore exceptions it could lead to unstable behavior.
// Exit and let the forever utility restart everything
function shutdownProcess()
{
process.exit(1); //exit with error
}
答案 2 :(得分:2)
您实际上应该尝试阻止ECONNREFUSED异常变为未被捕获:
var request = require( 'request' );
app.all( '/proxy/*', function( req, res ){
req.pipe( request({
url: config.backendUrl + req.params[0],
qs: req.query,
method: req.method
}))
.on('error', err => {
const msg = 'Error on connecting to the webservice.';
console.error(msg, err);
res.status(500).send(msg);
})
.pipe( res );
});
如果你得到一个实际的未捕获异常,那么你应该让应用程序死掉。