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

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

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

感谢阅读。

904661 次浏览

我尝试使用请求模块,并能够打印出该页的主体相当容易。不幸的是,以我所拥有的技能,我帮不了你。

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

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

我想这能解决你的问题。

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会被解析为url.parse();或调用http.get(options, callback),其中options

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

更新

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

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


});

的另一个常见错误来源

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

当我从开发环境转到生产环境时,我得到了这个错误。我痴迷于把https://放在所有链接上。这是不必要的,所以对某些人来说可能是一种解决方案。

我的问题是我的OS X (Mavericks) DNS服务需要重新启动。在卡特琳娜和大苏尔DNS缓存可以清除:

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

旧macOS版本在这里看到的

请注意,如果您引用的域宕机了,也会发生此问题。现在已经不存在了。)

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

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

我的代码中没有:

req.end();

(NodeJs V: 5.4.0) 一旦添加到req.end();行之上,我就能够消除错误,并为我工作得很好

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

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

https = require('https');


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


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

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

请求错误:getaddrinfo EAI_AGAIN localhost:3000:80 < /代码> < / p >

下面是一个完整的代码片段

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

如果你仍然面临代理设置的检查,对我来说,这是代理设置缺失,无法发出直接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
};

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

我用这个修正了这个错误

$ 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

尝试使用服务器IP地址而不是主机名。 这对我很管用。

我的问题是,我们正在解析url和生成http_options http.request();

我用的是request_url。主机已经有端口号和域名,所以必须使用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;

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

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

应该是

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

因此。com或。net等之后的任何内容都应该移动到path parameter value

在我的情况下,问题是一个畸形的URL。 URL中有双斜杠