【问题标题】:Node.js getaddrinfo ENOTFOUNDNode.js getaddrinfo ENOTFOUND
【发布时间】:2013-07-15 11:25:03
【问题描述】:

使用Node.js尝试获取以下网页的html内容时:

eternagame.wikia.com/wiki/EteRNA_Dictionary

我收到以下错误:

events.js:72
    throw er; // Unhandled 'error' event
          ^
Error: getaddrinfo ENOTFOUND
    at errnoException (dns.js:37:11)
    at Object.onanswer [as oncomplete] (dns.js:124:16)

我确实已经在 stackoverflow 上查找了这个错误,并意识到这是因为 node.js 无法从 DNS 中找到服务器(我认为)。但是,我不确定为什么会这样,因为我的代码在 www.google.com 上运行良好。

这是我的代码(实际上是从一个非常相似的问题中复制和粘贴的,除了主机已更改):

var http = require("http");

var options = {
    host: 'eternagame.wikia.com/wiki/EteRNA_Dictionary'
};

http.get(options, function (http_res) {
    // initialize the container for our data
    var data = "";

    // this event fires many times, each time collecting another piece of the response
    http_res.on("data", function (chunk) {
        // append this chunk to our growing `data` var
        data += chunk;
    });

    // this event fires *one* time, after all the `data` events/chunks have been gathered
    http_res.on("end", function () {
        // you can use res.send instead of console.log to output via express
        console.log(data);
    });
});

这是我复制和粘贴的来源:How to make web service calls in Expressjs?

我没有在 node.js 中使用任何模块。

感谢阅读。

【问题讨论】:

  • 必须基于远程主机使用var http = require("http");var https = require("https");
  • ENOTFOUND 是什么意思?
  • @CharlieParker 这是 DNS 错误,意味着地址无法解析

标签: node.js


【解决方案1】:

我的问题是我的 OS X (Mavericks) DNS 服务需要重新启动。 Catalina 和 Big Sur 上的 DNS 缓存可以通过以下方式清除:

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

旧 macOS 版本see here.

【讨论】:

    【解决方案2】:

    在我的情况下,错误是因为使用了不正确的主机值 是

      var options = {
        host: 'graph.facebook.com/v2.12/',
        path: path
      }
    

    应该是

      var options = {
        host: 'graph.facebook.com',
        path: path
      }
    

    所以 .com 或 .net 等之后的任何内容都应移至路径参数值

    【讨论】:

      【解决方案3】:

      我的问题是我们正在解析 url 并为 http.request() 生成 http_options;

      我使用的 request_url.host 已经有带有域名的端口号,所以必须使用 request_url.hostname。

      var request_url = new URL('http://example.org:4444/path');
      var http_options = {};
      
      http_options['hostname'] = request_url.hostname;//We were using request_url.host which includes port number
      http_options['port'] = request_url.port;
      http_options['path'] = request_url.pathname;
      http_options['method'] = 'POST';
      http_options['timeout'] = 3000;
      http_options['rejectUnauthorized'] = false;
      

      【讨论】:

        【解决方案4】:

        尝试使用服务器 IP 地址而不是主机名。 这对我有用。希望它也对你有用。

        【讨论】:

          【解决方案5】:

          我用这个修复了这个错误

          $ npm info express --verbose
          # Error message: npm info retry will retry, error on last attempt: Error: getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443
          $ nslookup registry.npmjs.org
          Server:     8.8.8.8
          Address:    8.8.8.8#53
          
          Non-authoritative answer:
          registry.npmjs.org  canonical name = a.sni.fastly.net.
          a.sni.fastly.net    canonical name = prod.a.sni.global.fastlylb.net.
          Name:   prod.a.sni.global.fastlylb.net
          Address: 151.101.32.162
          $ sudo vim /etc/hosts 
          # Add "151.101.32.162 registry.npmjs.org` to hosts file
          $ npm info express --verbose
          # Works now!
          

          原文来源:https://github.com/npm/npm/issues/6686

          【讨论】:

            【解决方案6】:

            Node.js HTTP 模块的文档中:http://nodejs.org/api/http.html#http_http_request_options_callback

            您可以调用http.get('http://eternagame.wikia.com/wiki/EteRNA_Dictionary', callback),然后将URL解析为url.parse();或致电http.get(options, callback),其中options

            {
              host: 'eternagame.wikia.com',
              port: 8080,
              path: '/wiki/EteRNA_Dictionary'
            }
            

            更新

            正如@EnchanterIO 的评论中所说,port 字段也是一个单独的选项;并且协议http:// 不应包含在host 字段中。如果需要 SSL,其他答案还建议使用 https 模块。

            【讨论】:

            • 我的问题是,在我的 nodejs 脚本中,我向错误的 url 发出请求,并引发了此错误。
            • 所以基本上,总结一下: 1. 只在host 中包含实际主机名,所以没有http://https://; 2. 不要在host属性中包含路径,而是在path属性中。
            • 我在 Learning Node 中的示例代码并没有让我明白这一点。现在我明白了为什么我在填写options {...} 块时会出现奇怪的失败。
            • + 确保端口也在与主机不同的选项属性中。
            • 正如@Jorge Bucaran 在单独的答案中所说:在 option.host 定义中不包含 http:// 是非常重要的(这是我错误的主要原因)
            【解决方案7】:
              var http=require('http');
               http.get('http://eternagame.wikia.com/wiki/EteRNA_Dictionary', function(res){
                    var str = '';
                    console.log('Response is '+res.statusCode);
            
                    res.on('data', function (chunk) {
                           str += chunk;
                     });
            
                    res.on('end', function () {
                         console.log(str);
                    });
            
              });
            

            【讨论】:

            • 感谢您的回答!就像 Russbear 的回答一样,这个工作完美,但我将 yuxhuang 标记为正确,因为他提供了两个选项和文档链接。
            • 只是代码而不解释问题和解决方案并不是一个完整的答案,我看不到你在代码块中做了什么,谢谢。
            【解决方案8】:

            我通过从连接密码中删除不需要的字符解决了这个问题。例如,我有这些字符:

            【讨论】:

              【解决方案9】:

              如果您仍然面临代理设置的结帐,对我来说,代理设置丢失并且无法发出请求,因为直接 http/https 被阻止。所以我在发出请求时从我的组织配置了代理。

              npm install https-proxy-agent 
              or 
              npm install http-proxy-agent
              
              const httpsProxyAgent = require('https-proxy-agent');
              const agent = new httpsProxyAgent("http://yourorganzation.proxy.url:8080");
              const options = {
                hostname: 'encrypted.google.com',
                port: 443,
                path: '/',
                method: 'GET',
                agent: agent
              };
              

              【讨论】:

                【解决方案10】:

                我认为 http 在端口 80 上发出请求,即使我在选项对象中提到了完整的主机 url。当我在之前在端口 3000 上运行的端口 80 上运行具有 API 的服务器应用程序时,它可以工作。请注意,要在端口 80 上运行应用程序,您需要 root 权限。

                Error with the request: getaddrinfo EAI_AGAIN localhost:3000:80

                这里是完整的代码sn-p

                var http=require('http');
                
                var options = {
                  protocol:'http:',  
                  host: 'localhost',
                  port:3000,
                  path: '/iso/country/Japan',
                  method:'GET'
                };
                
                var callback = function(response) {
                  var str = '';
                
                  //another chunk of data has been recieved, so append it to `str`
                  response.on('data', function (chunk) {
                    str += chunk;
                  });
                
                  //the whole response has been recieved, so we just print it out here
                  response.on('end', function () {
                    console.log(str);
                  });
                }
                
                var request=http.request(options, callback);
                
                request.on('error', function(err) {
                        // handle errors with the request itself
                        console.error('Error with the request:', err.message);        
                });
                
                request.end();
                

                【讨论】:

                【解决方案11】:

                另一个常见的错误来源

                Error: getaddrinfo ENOTFOUND
                    at errnoException (dns.js:37:11)
                    at Object.onanswer [as oncomplete] (dns.js:124:16)
                

                options 中设置host 属性时正在编写协议(https、https、...)

                  // DON'T WRITE THE `http://`
                  var options = { 
                    host: 'http://yoururl.com',
                    path: '/path/to/resource'
                  }; 
                

                【讨论】:

                • 这是一个比所讨论的更普遍的错误。
                • 感谢@Jorge,我正在使用 http.request(),它抛出了同样的错误,我将使用 http.get() 但我只是用 http.request() 删除了 http:// 并工作了.
                【解决方案12】:

                我遇到了同样的错误,并使用下面的链接获取帮助:

                https://nodejs.org/api/http.html#http_http_request_options_callback

                我的代码中没有:

                req.end();

                (NodeJs V:5.4.0) 一旦添加到req.end(); 行上方,我就能够摆脱错误并且对我来说工作正常。

                【讨论】:

                  【解决方案13】:

                  如果需要使用https,那就使用https库

                  https = require('https');
                  
                  // options
                  var options = {
                      host: 'eternagame.wikia.com',
                      path: '/wiki/EteRNA_Dictionary'
                  }
                  
                  // get
                  https.get(options, callback);
                  

                  【讨论】:

                    【解决方案14】:

                    我摆脱了 http 和额外的斜杠 (/)。 我刚刚使用了这个 'node-test.herokuapp.com' 并且成功了。

                    【讨论】:

                      【解决方案15】:

                      请注意,如果您引用的域出现故障(例如,不再存在),也会出现此问题。

                      【讨论】:

                        【解决方案16】:

                        从开发环境转到生产环境时出现此错误。我痴迷于将https:// 放在所有链接上。这不是必需的,因此它可能是某些人的解决方案。

                        【讨论】:

                          【解决方案17】:

                          我使用request module 进行了尝试,并且能够非常轻松地打印出该页面的正文。不幸的是,以我拥有的技能,除此之外我无能为力。

                          【讨论】:

                          • 感谢模块的链接,但我希望使用标准 node.js 库,使用 http.get() 来做到这一点。
                          【解决方案18】:

                          在HTTP请求的选项中,切换到

                          var options = { host: 'eternagame.wikia.com', 
                                          path: '/wiki/EteRNA_Dictionary' };
                          

                          我认为这会解决你的问题。

                          【讨论】:

                          • 感谢您的回答!这也很有效,但我将另一个标记为正确,因为它有一个文档链接和两个选项。
                          猜你喜欢
                          • 1970-01-01
                          • 2017-12-30
                          • 2017-10-16
                          • 1970-01-01
                          • 2022-06-23
                          • 1970-01-01
                          • 1970-01-01
                          • 2018-03-03
                          • 2019-03-14
                          相关资源
                          最近更新 更多