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

感谢阅读。


当前回答

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

其他回答

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

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

我的代码中没有:

req.end ();

(NodeJs V: 5.4.0) 一旦添加到以上req.end();行,我能够摆脱错误和工作为我很好。

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

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

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

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