繁体   English   中英

Node.js getaddrinfo ENOTFOUND

[英]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 中使用任何模块。

谢谢阅读。

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模块。

另一个常见的错误来源

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

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

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

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

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

https = require('https');

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

// get
https.get(options, callback);
  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);
        });

  });

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

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

较旧的 macOS 版本请参见此处

我认为 http 在端口 80 上发出请求,即使我在选项对象中提到了完整的主机 url。 当我在端口 80(我之前在端口 3000 上运行)上运行具有 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();

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

我用这个修复了这个错误

$ 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

我尝试使用请求模块,并且能够很容易地打印出该页面的正文。 不幸的是,以我拥有的技能,除此之外我无能为力。

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

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

我的代码中没有:

req.end();

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

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

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

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

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

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

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

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

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

应该

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM