node.js-错误:g的nodejs中的getaddrinfo ENOTFOUND

我正在节点上运行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中应用的最佳入门教程?

nnm asked 2020-07-11T17:00:35Z
9个解决方案
83 votes

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,但是 许多在那里。

Karol answered 2020-07-11T17:00:56Z
14 votes

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

Ben Kim answered 2020-07-11T17:01:16Z
12 votes

检查这样的主机文件

##
# 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
neoerol answered 2020-07-11T17:01:36Z
7 votes

我无法解释为什么,但将Windows 10上mongodb.MongoClient.connect()方法上的url参数从2962004282691691400400更改为127.0.0.1可以解决此问题。

jay answered 2020-07-11T17:01:58Z
2 votes

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

希望这个能对您有所帮助 -

Naren Chejara answered 2020-07-11T17:02:22Z
0 votes

我在Amazon服务器上遇到了同样的问题,我将代码更改为此

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

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

Adiii answered 2020-07-11T17:02:48Z
0 votes

当我尝试安装新的离子应用程序时,出现以下相同错误,我尝试了许多资料,发现在用户环境和系统环境中犯的错误不必要地包含了PROXY值。我删除了用户变量[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
Dr NVS answered 2020-07-11T17:03:13Z
-1 votes
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%)
Er.Subhendu Kumar Pati answered 2020-07-11T17:03:29Z
-3 votes

我遇到了同样的问题,经过反复试验发现我的/private/etc/hosts文件自定义引起了该问题。

我的/private/etc/hosts DNS被覆盖,因此,一个简单的ping失败了:

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

解决了/private/etc/hosts文件的配置后,对localhost进行ping操作成功,并且不再收到您看到的错误:

# Fatal error: getaddrinfo ENOTFOUND
Hady answered 2020-07-11T17:04:03Z
translate from https://stackoverflow.com:/questions/23259697/error-getaddrinfo-enotfound-in-nodejs-for-get-call