Error: getaddrinfo ENOTFOUND in nodejs for get call

node.jsMongodbbackbone.jsExpressRestify

node.js Problem Overview


I am running a web server on node the code for which is given below

var restify = require('restify');

var server = restify.createServer();

var quotes = [
  { author : 'Audrey Hepburn', text : "Nothing is impossible, the word itself says 'I'm possible'!"},
  { author : 'Walt Disney', text : "You may not realize it when it happens, but a kick in the teeth may be the best thing in the world for you"},
  { author : 'Unknown', text : "Even the greatest was once a beginner. Don't be afraid to take that first step."},
  { author : 'Neale Donald Walsch', text : "You are afraid to die, and you're afraid to live. What a way to exist."}
];

server.get('/', function(req, res) {
  res.json(quotes);
});

server.get('/quote/random', function(req, res) {
  var id = Math.floor(Math.random() * quotes.length);
  var q = quotes[id];
  res.json(q);
});

server.get('/quote/:id', function(req, res) {
  if(quotes.length <= req.params.id || req.params.id < 0) {
    res.statusCode = 404;
    return res.send('Error 404: No quote found');
  }

  var q = quotes[req.params.id];
  res.json(q);
});

server.listen(process.env.PORT || 3011);

And then i want to do a get request in the following code

var https = require('http');

/**
 * HOW TO Make an HTTP Call - GET
 */
// options for GET
var optionsget = {
    host : 'http://localhost',
    port : 3010,
    path : '/quote/random', // the rest of the url with parameters if needed
    method : 'GET' // do GET
};

console.info('Options prepared:');
console.info(optionsget);
console.info('Do the GET call');

// do the GET request
var reqGet = https.request(optionsget, function(res) {
    console.log("statusCode: ", res.statusCode);
    // uncomment it for header details
//  console.log("headers: ", res.headers);


    res.on('data', function(d) {
        console.info('GET result:\n');
        process.stdout.write(d);
        console.info('\n\nCall completed');
    });

});

reqGet.end();
reqGet.on('error', function(e) {
    console.error(e);
});

I am just starting with node and i dont even know if this is the right way. I want to test the performance of express and restify.I have done a apache benchmark test for the server code i wrote and found contradicting results that restify is better.So i want to test out some more by making calls to remote services and then later read write to mongodb.The above code is my starting point.I am getting the error

{ [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' }

Am i atleast heading in the write direction? What is the right way to the kind of tests that i want to? Why did i get the result restify faster than express? Can anyone guide me to the best starting point tutorials for application in node/express/backbone and mongodb?

node.js Solutions


Solution 1 - node.js

getaddrinfo ENOTFOUND means client was not able to connect to given address. Please try specifying host without http:

var optionsget = {
    host : 'localhost',
    port : 3010,
    path : '/quote/random', // the rest of the url with parameters if needed
    method : 'GET' // do GET
};

Regarding learning resources, you won't go wrong if you start with http://www.nodebeginner.org/ and then go through some good book to get more in-depth knowledge - I recommend Professional Node.js , but there's many out there.

Solution 2 - node.js

Check host file which like this

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1	localhost
255.255.255.255	broadcasthost

Solution 3 - node.js

for me it was because in /etc/hosts file the hostname is not added

Solution 4 - node.js

I can't explain why, but changing url parameter from having localhost to 127.0.0.1 on mongodb.MongoClient.connect() method on Windows 10 solved the issue.

Solution 5 - node.js

I also had same problem and I fixed it by using right proxy. Please double check your proxy settings if you are using proxy network.

Hope this will help you -

Solution 6 - node.js

> i have same issue with Amazon server i change my code to this

var connection = mysql.createConnection({
	localAddress     : '35.160.300.66',
	user     : 'root',
	password : 'root',
	database : 'rootdb',
});

check mysql node module https://github.com/mysqljs/mysql

Solution 7 - node.js

My problem was that my endpoint was http://tenant1.localhost. While this somehow works in the browser, it did not with node.

I had to add the following line to /etc/hosts

127.0.0.1 tenant1.localhost

Solution 8 - node.js

var http = require('http');

  var options = {     
      host: 'localhost',
      port: 80,
      path: '/broadcast'
    };

  var requestLoop = setInterval(function(){

      http.get (options, function (resp) {
        resp.on('data', function (d) {
          console.log ('data!', d.toString());
        });
        resp.on('end', function (d) {
           console.log ('Finished !');
        });
      }).on('error', function (e) {
          console.log ('error:', e);
      });
  }, 10000);

var dns = require('dns'), cache = {};
dns._lookup = dns.lookup;
dns.lookup = function(domain, family, done) {
    if (!done) {
        done = family;
        family = null;
    }

    var key = domain+family;
    if (key in cache) {
        var ip = cache[key],
            ipv = ip.indexOf('.') !== -1 ? 4 : 6;

        return process.nextTick(function() {
            done(null, ip, ipv);
        });
    }

    dns._lookup(domain, family, function(err, ip, ipv) {
        if (err) return done(err);
        cache[key] = ip;
        done(null, ip, ipv);
    });
};

// Works fine (100%)

Solution 9 - node.js

When I tried to install a new ionic app, I got the same error as follows, I tried many sources and found the mistake made in User Environment and System Environment unnecessarily included the PROXY value. I removed the ```user variables http://host:port PROXY

system Variables http_proxy http://username:password@host:port ```` and now it is working fine without trouble.

[ERROR] Network connectivity error occurred, are you offline?

        If you are behind a firewall and need to configure proxy settings, see: https://ion.link/cli-proxy-docs

        Error: getaddrinfo ENOTFOUND host host:80

Solution 10 - node.js

I had a similar issue and it turned out the issue was with my proxy password.

The error was:

> "Start npm npm ERR! code ENOTFOUND npm ERR! errno ENOTFOUND npm ERR! > network request to https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz > failed, reason: getaddrinfo ENOTFOUND myUsername npm ERR! network This is > a problem related to network connectivity. npm ERR! network In most > cases you are behind a proxy or have bad network settings. npm ERR! > network npm ERR! network If you are behind a proxy, please make sure > that the npm ERR! network 'proxy' config is set properly. See: 'npm > help config'

It turned out the interesting part was this one :

> failed, reason: getaddrinfo ENOTFOUND myUsername

My password, that was used in the proxy, had special characters like ! and #. After I used percent encoding : https://en.wikipedia.org/wiki/Percent-encoding I was able to run npm install. Before this I tried with a wrong password and a received a 407 error.

Solution 11 - node.js

I was running into the same problem and after some trial and error discovered that my hosts file customisation was causing the problem.

My localhost DNS was overwritten and because of that a simple ping was failing:

$ ping http://localhost
# ping: cannot resolve http://localhost: Unknown host

Once I resolved the configuration of /private/etc/hosts file, pinging localhost successfully worked, and was no longer getting the error that you were seeing:

# Fatal error: getaddrinfo ENOTFOUND

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnnmView Question on Stackoverflow
Solution 1 - node.jsKarolView Answer on Stackoverflow
Solution 2 - node.jsneoerolView Answer on Stackoverflow
Solution 3 - node.jsBen KimView Answer on Stackoverflow
Solution 4 - node.jsJay WickView Answer on Stackoverflow
Solution 5 - node.jsNaren ChejaraView Answer on Stackoverflow
Solution 6 - node.jsAdiiiView Answer on Stackoverflow
Solution 7 - node.jsT3rm1View Answer on Stackoverflow
Solution 8 - node.jsEr.Subhendu Kumar PatiView Answer on Stackoverflow
Solution 9 - node.jsDr NVSView Answer on Stackoverflow
Solution 10 - node.jsPopescu MView Answer on Stackoverflow
Solution 11 - node.jsHadyView Answer on Stackoverflow