ChatGPT解决这个技术问题 Extra ChatGPT

Node.js getaddrinfo ENOTFOUND

使用 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 错误,意味着无法解析地址

f
felixwong8

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.parse() 解析 URL;或调用 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:// 是非常重要的(这是我错误的主要原因)
J
Jorge Bucaran

另一个常见的错误来源

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:// 并开始工作。
R
Russbear

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

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

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


感谢你的回答!这也很有效,但我将另一个标记为正确,因为它有一个文档链接和两个选项。
v
vhs

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

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

旧 macOS 版本 see here


A
Andrew

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

https = require('https');

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

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

s
sheshu kurnool
  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 的正确,因为他提供了两个选项和文档链接。
只是编码而不解释问题和解决方案并不是一个完整的答案,我看不到你在代码块中做了什么,谢谢。
M
Mahtab Alam

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

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

这是一个完整的代码片段

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

这个答案的重要部分是协议。 nodejs http 不支持带有 host: https://server.com 的完整 uri,这里也提到了 stackoverflow.com/a/28385129/432903
e
eyllanesc

我用这个修复了这个错误

$ 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


M
Mauvis Ledford

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


k
kashlo

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

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

应该

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

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


我也是!删除了 https:// 并且效果很好。非常感谢。
m
mayorbyrne

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


感谢模块的链接,但我希望使用 http.get() 使用标准 node.js 库来执行此操作。
p
petur

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


S
Sayuri Mizuguchi

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

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

我的代码中没有:

req.end();

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


H
Hemantkumar Gaikwad

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


谢谢你的提示!
U
Uday Reddy

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


A
Ansar Ahmed

如果您仍然面临代理设置的结帐,对我来说,代理设置丢失并且无法发出请求,因为直接 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
};

S
Shota

我通过从连接密码中删除不需要的字符解决了这个问题。例如,我有这些字符: <##% 并且它导致了问题(很可能哈希标签是问题的根本原因)。


A
Arjun J Gowda

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

S
Sterling Diaz

就我而言,问题是 URL 格式错误。我在 URL 中有双斜杠。