Node.js getaddrinfo ENOTFOUND

node.js Node.js

樱阿飞

2020-03-20

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

这是我复制和粘贴的来源:如何在Expressjs中进行Web服务调用?

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

谢谢阅读。

第2453篇《Node.js getaddrinfo ENOTFOUND》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

7个回答
神乐小胖 2020.03.20

Try using the server IP address rather than the hostname. This worked for me. Hope it will work for you too.

番长樱梅 2020.03.20

I got this issue resolved by removing non-desirable characters from the password for the connection. For example, I had these characters: <##% and it caused the problem (most probably hash tag was the root cause of the problem).

番长 2020.03.20

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

I was not having in my code:

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.

猴子蛋蛋 2020.03.20

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

Near逆天 2020.03.20

I got this error when going from development environment to production environment. I was obsessed with putting https:// on all links. This is not necessary, so it may be a solution for some.

阳光村村 2020.03.20

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

神乐阳光 2020.03.20

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

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android