[英]Node.js application quits if other server is unreachable. How do I prevent my application from stopping?
I am writing an application that regularly checks in with a remote server to check in for new versions of software. 我正在编写一个应用程序,该应用程序定期与远程服务器签入,以签入新版本的软件。 It's my first attempt in node.js so please bear with me.
这是我第一次尝试使用node.js,请耐心等待。 When the server is up and running, the program runs totally fine.
当服务器启动并运行时,程序运行完全正常。
However, when the server is not running, the program quits with the error: 但是,当服务器未运行时,程序会退出并显示错误消息:
events.js:72
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
From what I've read, I believe I have set up my code to handle errors correctly, by including a response.on('error', ...) part of my callback. 通过阅读,我相信我已经通过在回调中包含response.on('error',...)部分来设置代码以正确处理错误。 However, as you can see from the error, it's not giving me the error console.logs that I tried to set up.
但是,从错误中可以看到,它没有给我尝试设置的错误console.logs。 Here's the part of my code that is not working, I can include other parts if need be, it's just a pretty big application at this point:
这是我的代码中不起作用的部分,如果需要的话,我可以包括其他部分,这只是一个非常大的应用程序:
var index = require ("./index");
var http = require ('http');
var fs = require ("fs");
var apps = [{
"name":"IDSocket",
"localVersion": "",
"hostVersion": "",
"downloadURL": ""
},
{
"name": "X-Wing vs. Tie Fighter",
"localVersion": "4.2.1",
"hostVersion": "",
"downloadURL": ""
}]
//checks host server for host version, stores as json object
function checkHost(hostinfo){
callback1 = function(response) {
var str = '';
var hostObject;
response.on('data', function (chunk) {
str += chunk;
});
response.on('error', function(err) {
console.log('problem with request: ' + err.message);
hostObject = "why, error? " + err.message
});
response.on('end', function () {
hostObject = str;
hostObject = JSON.parse(hostObject);
apps[0]["hostVersion"] = hostObject[0]["version"];
apps[0]["downloadURL"] = hostObject[0]["downloadURL"];
apps[1]["hostVersion"] = hostObject[1]["version"]
apps[1]["downloadURL"] = hostObject[1]["downloadURL"]
console.log(apps[0]["hostVersion"] + ": recieved IDSocket host version");
index.everyFourHours();
});
}
http.request(hostinfo, callback1).end();
};
Thanks in advance for any helping me understand what's going on here! 预先感谢您对我的理解,这对我有什么帮助!
EDIT: things I have tried so far: 编辑:到目前为止我尝试过的事情:
try block on both the http.request line as well as the whole callback function with catch blocks that just console.log the error 尝试在http.request行以及整个回调函数上使用带有console.log错误的catch块的块
process.on('uncaughtException' process.on( 'uncaughtException'
domain.create & domain.run domain.create和domain.run
You should be adding an error handler to instance of your http.request
. 您应该在
http.request
实例中添加错误处理程序。
For example, you could change your http.request
call to: 例如,您可以将
http.request
调用更改为:
var req = http.request(hostinfo, callback1);
req.on('error', function(err) { console.log("ERROR: " + err.message); });
req.end();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.