Node.js getaddrinfo ENOTFOUND

398

当使用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模块。

谢谢阅读。


https://dev59.com/KnVD5IYBdhLWcg3wRpaX#11921896 - Arfan Mirza
根据远程主机,必须使用 var http = require("http");var https = require("https"); - prayagupa
7
"ENOTFOUND"是什么意思? - Charlie Parker
8
@CharlieParker 这是 DNS 错误,意味着地址无法解析。 - Greedo
19个回答

3

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

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

应该是

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

因此,.com或.net等后面的任何内容都应该移动到路径参数值中。


我也是!去掉了 https:// 之后,它运行得很好。非常感谢。 - Adrian Smith

1
当我从开发环境切换到生产环境时,出现了这个错误。我一直坚持在所有链接上添加 https://。但这并不是必要的,所以这可能是一种解决方案。

1
我尝试使用request模块进行操作,很容易地打印出了该页面的正文。不幸的是,基于我的技能水平,除此之外我无法提供更多帮助。

谢谢提供模块的链接,但我希望能够使用标准的node.js库,使用http.get()方法来完成。 - Vineet Kosaraju

1

1

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


0
如果您仍然遇到代理设置的结帐问题,对我来说,缺少代理设置并且无法直接进行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
};

0
我通过从连接密码中删除不必要的字符来解决了这个问题。例如,我有这些字符:<##%,它导致了问题(很可能是井号是问题的根本原因)。

0

我去掉了http和额外的斜杠(/)。我只使用了这个“node-test.herokuapp.com”,它可以工作。


0

我的问题是我们正在解析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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接