Node.js getaddrinfo ENOTFOUND

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17690803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:03:10  来源:igfitidea点击:

Node.js getaddrinfo ENOTFOUND

node.js

提问by Vineet Kosaraju

When using Node.js to try and get the html content of the following web page:

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

eternagame.wikia.com/wiki/EteRNA_Dictionary

eternagame.wikia.com/wiki/EteRNA_Dictionary

I get the following error:

我收到以下错误:

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)

I did already look up this error on stackoverflow, and realized that this is because node.js cannot find the server from DNS (I think). However, I am not sure why this would be, as my code works perfectly on www.google.com.

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

Here is my code (practically copied and pasted from a very similar question, except with the host changed):

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

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);
    });
});

Here is the source where I copied and pasted from : How to make web service calls in Expressjs?

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

I am not using any modules with node.js.

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

Thanks for reading.

谢谢阅读。

回答by yuxhuang

In Node.jsHTTPmodule's documentation: http://nodejs.org/api/http.html#http_http_request_options_callback

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

You can either call http.get('http://eternagame.wikia.com/wiki/EteRNA_Dictionary', callback), the URL is then parsed with url.parse(); or call http.get(options, callback), where optionsis

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

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

Update

更新

As stated in the comment by @EnchanterIO, the portfield is also a separate option; and the protocol http://shouldn't be included in the hostfield. Other answers also recommends the use of httpsmodule if SSL is required.

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

回答by Jorge Bucaran

Another common source of error for

另一个常见的错误来源

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

is writing the protocol(https, https, ...) when setting the hostproperty in options

在设置属性时正在编写协议(https,https,...)hostoptions

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

回答by Russbear

in the options for the HTTP request, switch it to

在 HTTP 请求的选项中,将其切换为

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

I think that'll fix your problem.

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

回答by sachin

  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);
        });

  });

回答by Andrew

If you need to use https, then use the https library

如果需要使用https,则使用https库

https = require('https');

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

// get
https.get(options, callback);

回答by aaaidan

My problem was that my OS X (Mavericks) DNS service needed to be rebooted.

我的问题是我的 OS X (Mavericks) DNS 服务需要重新启动

回答by Mahtab Alam

I think http makes request on port 80, even though I mentioned the complete host url in options object. When I run the server application which has the API, on port 80, which I was running previously on port 3000, it worked. Note that to run an application on port 80 you will need root privilege.

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

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

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

Here is a complete code snippet

这是一个完整的代码片段

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();

回答by Mertcan Diken

I fixed this error with this

我用这个修复了这个错误

$ 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!

Original source: https://github.com/npm/npm/issues/6686

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

回答by Mauvis Ledford

Note that this issue can also occur if the domain you are referencing goes down (EG. no longer exists.)

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

回答by sudeep patel

I was getting the same error and used below below link to get help:

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

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

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

I was not having in my code:

我的代码中没有:

req.end();

req.end();

(NodeJs V: 5.4.0)once added above req.end();line, I was able to get rid of the error and worked fine for me.

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