简体   繁体   English

如果其他服务器无法访问,则Node.js应用程序退出。 如何防止我的应用程序停止?

[英]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();

Read here for more info. 在这里阅读更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何防止人们从检查器中使 socket node.js 服务器过载? - How do I prevent people from overloading a socket node.js server from inspector? 如何从 static 网页链接到在 80 以外的端口上侦听的 node.js 应用程序? - How can I link to a node.js application listening on a port other than 80 from a static webpage? 如何防止我的 node.js 错误中间件 function 在随后的 function 调用中失败? - How do I prevent my node.js error middleware function from failing on subsequent function calls? 在 Node.js 中,如何从我的其他文件中“包含”函数? - In Node.js, how do I "include" functions from my other files? 一旦进程完成,防止 node.js 应用程序退出 - Prevent node.js application from exiting once the process is done 如何通过客户端JavaScript从我的node.js服务器访问数据? - How do I access data from my node.js server from client javascript? 将我的Socket.io和node.js应用程序上传到服务器 - uploading my Socket.io and node.js application to a server 使用node.js将3D应用程序转换为服务器...可能吗? - Turning my 3D application into a server with node.js… Possible? 从其他应用程序调用Node.js函数 - Calling Node.js function from other application 如何在我的节点 js 应用程序中显示来自 MongoDB Atlas 的数据? - How do i display my data from MongoDB Atlas in my node js application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM