【问题标题】:Node Js Issue: getaddrinfo ENOTFOUND节点 Js 问题:getaddrinfo ENOTFOUND
【发布时间】:2018-01-16 07:11:14
【问题描述】:

我正在编写一个节点应用程序来向外部 URL 发出 https 请求。我在公司代理后面,我已经设置了

>npm config set proxy http://proxy.sample.example.org:8080
>npm config set https-proxy http://proxy.sample.example.org:8080

我能够毫无问题地下载节点依赖项。但是,当我尝试点击外部 URL 时,它会引发以下错误。

    { Error: getaddrinfo ENOTFOUND example.net example.net:443
    at errnoException (dns.js:28:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
    code: 'ENOTFOUND',
    errno: 'ENOTFOUND',
    syscall: 'getaddrinfo',
    hostname: 'example.net',
    host: 'example.net',
    port: 443 }

这同样适用于 google.com。代码如下。

var request = require('request');
request.get(   
    {
    url : connUrl,//This is set to be https://example.net
    },function (error, response, body) {
       //Print response code
        console.log(body);
        console.log(error);
      });

谁能告诉我哪里出错了

【问题讨论】:

  • 因为你的代理没有为节点应用设置。设置代理

标签: node.js npm proxy


【解决方案1】:

对于Requests 模块,您可以使用proxy 选项设置代理。 试试这样的,

request.get({
      proxy: "proxy",
      url : connUrl,//This is set to be https://example.net
    },function (error, response, body) {
       //Print response code
        console.log(body);
        console.log(error);
});

您也可以设置http_proxy 和/或https_proxy 环境变量。

【讨论】:

    【解决方案2】:

    使用用户名和密码设置 npm 代理。 在这种情况下使用字母数字密码是安全的 (在大多数公司环境中,代理都是使用用户名和密码设置的)

    npm config set proxy http://username:password@10.81.82.100:80
    npm config set https-proxy https://username:password@10.81.82.100:80
    

    设置不带用户名和密码的npm代理

    npm config set proxy http://proxy.company.com:8080
    npm config set https-proxy http://proxy.company.com:8080
    

    添加@Malice的回答,使用环境变量设置临时代理

    set http_proxy=http://username:password@10.81.82.100:80
    set https_proxy=https://username:password@10.81.82.100:80
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-03
      • 2020-09-20
      • 2016-12-02
      • 1970-01-01
      • 2013-07-15
      • 2017-10-16
      • 1970-01-01
      • 2022-06-23
      相关资源
      最近更新 更多