小编典典

Node.js getaddrinfo ENOTFOUND

node.js

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

谢谢阅读。


阅读 401

收藏
2020-07-07

共1个答案

小编典典

Node.jsHTTP模块的文档中: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,其他答案也建议使用模块。

2020-07-07