node.js在发出http请求时区分错误 [英] node.js distinguishing errors when making http request

查看:124
本文介绍了node.js在发出http请求时区分错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的node.js应用程序使用 http.request 到REST API http://army.gov/launch-nukes 我需要区分三种可能的情况:

My node.js application is using http.request to the REST API http://army.gov/launch-nukes and I need to distinguish between three possible cases:


  • 成功 - 服务器回答是肯定的。我知道我的敌人被摧毁。

  • 失败 - 我从服务器收到错误,或者无法连接到服务器。我还有敌人。

  • 未知 - 建立与服务器的连接后,我发送了请求 - 但是不确定发生了什么。这可能意味着请求从未进入服务器,或者服务器对我的响应从未成功。我可能会或者可能不会刚刚开始一场世界大战。

  • Success -- The server replies in the affirmative. I know my enemies are destroyed.
  • Failure -- Either I have received error from the server, or was unable to connect to server. I still have enemies.
  • Unknown -- After establishing a connection to the server, I have sent the request -- but not sure what happened. This could mean the request never made it to the server, or the server response to me never made it. I may or may not have just started a world war.

正如你所看到的,区分<$非常重要c $ c>失败和未知案例,因为它们会产生截然不同的后果和不同的行动。

As you can see, it's very important for me to distinguish the Failure and Unknown case, as they have very different consequences and different actions I need to take.

我也非常想使用http Keep-Alive - 我可以说,我是一个战争贩子,并计划在爆发时提出大量请求(然后没有很长一段时间)

I would also very much like to use http Keep-Alive -- as what can I say, I'm a bit of a war-monger and plan on making lots of requests in bursts (and then nothing for long periods of time)

-

问题的核心是如何分离连接-error / time-out(这是一个失败)来自请求发送到线路后发生的错误/超时(这是一个未知)。

The core of the question is how to separate a connection-error/time-out (which is a Failure) from an error/timeout that occurs after the request is put on the wire (which is an Unknown).

在伪代码逻辑中我想要这个:

In psuedo-code logic I want this:

var tcp = openConnectionTo('army.gov') // start a new connection, or get an kept-alive one
tcp.on('error', FAILURE_CASE);
tcp.on('connectionEstablished',  function (connection) {

       var req = connection.httpGetRequest('launch-nukes');
       req.on('timeout', UNKNOWN_CASE);
       req.on('response', /* read server response and decide FAILURE OR SUCCESS */);
   }
)


推荐答案

以下是一个例子:

var http = require('http');

var options = {
  hostname: 'localhost',
  port: 7777,
  path: '/',
  method: 'GET'
};

var req = http.request(options, function (res) {
  // check the returned response code
  if (('' + res.statusCode).match(/^2\d\d$/)) {
    // Request handled, happy
  } else if (('' + res.statusCode).match(/^5\d\d$/))
    // Server error, I have no idea what happend in the backend
    // but server at least returned correctly (in a HTTP protocol
    // sense) formatted response
  }
});

req.on('error', function (e) {
  // General error, i.e.
  //  - ECONNRESET - server closed the socket unexpectedly
  //  - ECONNREFUSED - server did not listen
  //  - HPE_INVALID_VERSION
  //  - HPE_INVALID_STATUS
  //  - ... (other HPE_* codes) - server returned garbage
  console.log(e);
});

req.on('timeout', function () {
  // Timeout happend. Server received request, but not handled it
  // (i.e. doesn't send any response or it took to long).
  // You don't know what happend.
  // It will emit 'error' message as well (with ECONNRESET code).

  console.log('timeout');
  req.abort();
});

req.setTimeout(5000);
req.end();

我建议您使用netcat玩它,即:。

I recommend you play with it using netcat, ie.:

$ nc -l 7777
// Just listens and does not send any response (i.e. timeout)

$ echo -e "HTTP/1.1 200 OK\n\n" | nc -l 7777
// HTTP 200 OK

$ echo -e "HTTP/1.1 500 Internal\n\n" | nc -l 7777
// HTTP 500

(等等......)

这篇关于node.js在发出http请求时区分错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆