Node.js getaddrinfo ENOTFOUND [英] Node.js getaddrinfo ENOTFOUND

查看:227
本文介绍了Node.js getaddrinfo ENOTFOUND的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Node.js尝试获取以下网页的html内容时:

When using Node.js to try and get the html content of the following web page:

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上完美地工作.

I did already look up this error on stackoverflow, and realized that this is because node.js cannot find the server from DNS (I think). However, I am not sure why this would be, as my code works perfectly on www.google.com.

这是我的代码(实际上是从一个非常类似的问题复制并粘贴的,除了更改了主机):

Here is my code (practically copied and pasted from a very similar question, except with the host changed):

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服务调用?

Here is the source where I copied and pasted from : How to make web service calls in Expressjs?

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

I am not using any modules with node.js.

感谢阅读.

推荐答案

Node.js HTTP模块的文档中:您可以调用http.get('http://eternagame.wikia.com/wiki/EteRNA_Dictionary', callback),然后使用url.parse()解析URL;或致电http.get(options, callback),其中options

You can either call http.get('http://eternagame.wikia.com/wiki/EteRNA_Dictionary', callback), the URL is then parsed with url.parse(); or call http.get(options, callback), where options is

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

更新

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

As stated in the comment by @EnchanterIO, the port field is also a separate option; and the protocol http:// shouldn't be included in the host field. Other answers also recommends the use of https module if SSL is required.

这篇关于Node.js getaddrinfo ENOTFOUND的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆