Node.js getaddrinfo ENOTFOUND

2019-01-02 23:21发布

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

eternagame.wikia.com/wiki/EteRNA_Dictionary

I get the following error:

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)

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

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

I am not using any modules with node.js.

Thanks for reading,

Vineet

标签: node.js
16条回答
淡お忘
2楼-- · 2019-01-02 23:30

If still you are facing checkout for proxy setting, for me it was the proxy setting which were missing and was not able to make the request as direct http/https are blocked. So i configured the proxy from my organization while making the request.

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
};
查看更多
放我归山
3楼-- · 2019-01-02 23:30

I got this issue resolved by removing non-desirable characters from the password for the connection. For example, I had these characters: <##% and it caused the problem (most probably hash tag was the root cause of the problem).

查看更多
你好瞎i
4楼-- · 2019-01-02 23:31

In Node.js HTTP module's documentation: http://nodejs.org/api/http.html#http_http_request_options_callback

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

Update

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.

查看更多
不美不萌又怎样
5楼-- · 2019-01-02 23:31

If you need to use https, then use the https library

https = require('https');

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

// get
https.get(options, callback);
查看更多
女痞
6楼-- · 2019-01-02 23:32

Note that this issue can also occur if the domain you are referencing goes down (EG. no longer exists.)

查看更多
放我归山
7楼-- · 2019-01-02 23:36
  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);
        });

  });
查看更多
登录 后发表回答