【问题标题】:Error: getaddrinfo ENOTFOUND in nodejs for get call错误:nodejs 中的 getaddrinfo ENOTFOUND 用于获取调用
【发布时间】:2014-06-09 04:41:03
【问题描述】:

我正在节点上运行一个 Web 服务器,其代码如下所示

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

然后我想在下面的代码中做一个获取请求

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

我只是从节点开始,我什至不知道这是否是正确的方法。 我想测试 express 和 restify 的性能。我对我编写的服务器代码进行了 apache 基准测试,发现restify更好的结果相互矛盾。所以我想通过调用远程服务然后稍后再测试一些读写mongodb。上面的代码是我的起点。我收到错误

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

我是否至少朝着写作方向前进?我想要的那种测试的正确方法是什么?为什么我得到的结果比快递快?谁能指导我在 node/express/backbone 和 mongodb 中应用的最佳起点教程?

【问题讨论】:

标签: node.js mongodb backbone.js express restify


【解决方案1】:

我遇到了类似的问题,后来发现问题出在我的代理密码上。

错误是:

"启动 npm npm ERR!code ENOTFOUND npm ERR!errno ENOTFOUND npm ERR! 网络请求到https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz 失败,原因:getaddrinfo ENOTFOUND myUsername npm ERR!网络这是 与网络连接有关的问题。 npm 错误!网络在大多数 如果您使用代理服务器或网络设置错误。 npm 错误! 网络 npm 错误!网络 如果您使用代理,请确保 那个 npm 错误!网络“代理”配置设置正确。见:'npm 帮助配置'

原来有趣的部分是这个:

失败,原因:getaddrinfo ENOTFOUND myUsername

我在代理中使用的密码包含特殊字符,例如!和 #。 在我使用百分比编码后:https://en.wikipedia.org/wiki/Percent-encoding 我能够运行 npm install。在此之前,我尝试输入错误的密码并收到 407 错误。

【讨论】:

    【解决方案2】:

    我的问题是我的端点是http://tenant1.localhost。虽然这以某种方式在浏览器中有效,但它不适用于节点。

    我必须将以下行添加到/etc/hosts

    127.0.0.1 tenant1.localhost
    

    【讨论】:

      【解决方案3】:

      当我尝试安装一个新的 ionic 应用程序时,我得到了同样的错误,如下所示, 我尝试了很多来源,发现在用户环境和系统环境中犯的错误不必要地包含了代理值。 我删除了```用户变量 http://host:port 代理

      系统变量 http_proxy http://username:password@host:port```` 现在它可以正常工作了。

      [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
      

      【讨论】:

        【解决方案4】:

        检查类似这样的主机文件

        ##
        # 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
        

        【讨论】:

        • 这为我解决了问题!经过几个小时的搜索,我发现我的mac上的/etc/hosts文件是空的!在mac上修改文件后,运行sudo killall -HUP mDNSResponder使修改生效!
        【解决方案5】:
        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%)
        

        【讨论】:

          【解决方案6】:

          我对亚马逊服务器有同样的问题,我将我的代码更改为这个

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

          检查mysql节点模块 https://github.com/mysqljs/mysql

          【讨论】:

            【解决方案7】:

            我无法解释原因,但是在 Windows 10 上的 mongodb.MongoClient.connect() 方法上将 url 参数从 localhost 更改为 127.0.0.1 解决了这个问题。

            【讨论】:

            • 类似的东西在 OSX sierra 上工作,我尝试连接 MongoClient.connect('mongodb://localhost:27017/meanhotel', function(err, db){......} 但是这有效: MongoClient.connect('mongodb://127.0.0.1:27017/meanhotel', function(err, db){......}
            【解决方案8】:

            我也遇到了同样的问题,我通过使用正确的代理解决了这个问题。 如果您使用的是代理网络,请仔细检查您的代理设置。

            希望这对你有帮助 -

            【讨论】:

              【解决方案9】:

              对我来说这是因为在 /etc/hosts 文件中没有添加主机名

              【讨论】:

              • 你添加了什么主机名?
              • @PerStröm 127.0.0.1 localhost
              【解决方案10】:

              我遇到了同样的问题,经过反复试验发现我的 hosts 文件自定义导致了问题。

              我的localhost DNS 被覆盖,因此简单的 ping 失败:

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

              一旦我解决了/private/etc/hosts 文件的配置,ping localhost 成功工作,并且不再收到您看到的错误:

              # Fatal error: getaddrinfo ENOTFOUND
              

              【讨论】:

              • 您无法使用端口 80 的 http 进行 ping。Ping 使用 ICMP 端口 1。但是您可以 ping localhost。
              【解决方案11】:

              getaddrinfo ENOTFOUND 表示客户端无法连接到给定地址。 请尝试指定不带 http 的主机:

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

              关于学习资源,如果您从http://www.nodebeginner.org/ 开始,然后阅读一些好书以获得更深入的知识,您不会出错 - 我推荐Professional Node.js,但那里有很多。

              【讨论】:

              • getaddrinfo ENOUTFOUND means client was not able to connect... -> 确保在使用终端或提示 ping 该地址时得到回复。
              • 如果您有localhost 作为您的主机,但它仍然无法正常工作,请检查您的\etc\hosts 文件是否有此行:127.0.0.1 localhost
              • 谢谢@karol mate
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2018-05-24
              • 2016-04-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-07-03
              • 2017-10-16
              相关资源
              最近更新 更多