如何跟踪节点网络代码中的错误? [英] How to track down error in node network code?

查看:130
本文介绍了如何跟踪节点网络代码中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图找出为什么我的代码由于此错误而失败:

I am trying to track down why my code is failing with this error:

 { Error: connect ECONNREFUSED 127.0.0.1:443
     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1082:14)
   errno: 'ECONNREFUSED',
   code: 'ECONNREFUSED',
   syscall: 'connect',
   address: '127.0.0.1',
   port: 443 }

,该错误的堆栈跟踪如下所示:

and the stack trace for that error looks like this:

 Error: connect ECONNREFUSED 127.0.0.1:443
     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1082:14)

我正在建立网络连接,但是我没有尝试连接到本地主机.

I am making a network connection, but I am not trying to connect to localhost.

我正在使用的网络代码在其他情况下也可以正常工作(并连接到正确的主机).

The network code I am using works just fine (and connects to the right host) in other contexts.

我使用的是brew install提供的节点11.6.0,看来brew"Cellar"目录结构中字符串"net.js"的唯一出现是在节点二进制文件本身中.换句话说,即使在

I'm using node 11.6.0 as supplied by brew install, and it looks like the only occurrence of the string "net.js" in the brew "Cellar" directory structure is in the node binary itself. In other words, even after

 brew install --build-from-source node

运行

 find /usr/local/Cellar/node/11.6.0 -type f | xargs fgrep -l net.js

仅找到一个文件:/usr/local/Cellar/node/11.6.0/bin/node并且该目录树中没有net.*文件.

only finds one file: /usr/local/Cellar/node/11.6.0/bin/node and there's no net.* files in that directory tree.

如果有关系,我的似乎触发此错误的代码如下:

If it matters, my code which seems to trigger this error looks like this:

  const get_json_with_auth= async (channel, url) => {
    const parsed_url= URL.parse(url);
   return new Promise((good, fail) => {
     let request= require('https').get({
       hostname: parsed_url.hostname,
       path: parsed_url.path,
       headers: {
         Authorization: get_auth('GET', channel, url)
       }
     });
     console.log('url', url);
     request.on('response', response=> {
       const chunks= [];
       response.on('data', data=> chunks.push(data));
       response.on('end', ()=> good(JSON.parse(Buffer.concat(chunks).toString())));
     });
     request.on('error', err=> fail(err));
   });
 };

(我在OSX高山脉上.)

(I'm on OSX high sierra here.)

但是,再次,我使用的url中的主机名不能解析为localhost(在其他情况下也可以正常工作).

But, once again, the host name in the url I'm using does not resolve to localhost (and works just fine in other contexts).

如何隔离这样的问题?

推荐答案

使用环境变量NODE_DEBUG ='net,tls,http,https'运行我的代码为我提供了足够的信息来识别(并隔离)问题.

Running my code with the environmental variable NODE_DEBUG='net,tls,http,https' gave me enough information to recognize (and isolate) the problem.

这篇关于如何跟踪节点网络代码中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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