Error: getaddrinfo ENOTFOUND in nodejs for get call

后端 未结 10 699
离开以前
离开以前 2020-11-30 00:24

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 = [         


        
相关标签:
10条回答
  • 2020-11-30 00:44

    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.

    0 讨论(0)
  • 2020-11-30 00:48

    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.

    0 讨论(0)
  • 2020-11-30 00:48

    Struggling for hours, couldn't afford for more.

    The solution that worked for me in less than 3 minutes was:

    npm install axios
    

    Code ended up even shorter:

    const url = `${this.env.someMicroservice.address}/v1/my-end-point`;
    
    const { data } = await axios.get<MyInterface[]>(url, {
      auth: {
        username: this.env.auth.user,
        password: this.env.auth.pass
      }
    });
    
    return data;
    
    0 讨论(0)
  • 2020-11-30 00:56

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

    0 讨论(0)
  • 2020-11-30 00:56
    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%)
    
    0 讨论(0)
  • 2020-11-30 00:57

    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 -

    0 讨论(0)
提交回复
热议问题