ChatGPT解决这个技术问题 Extra ChatGPT

错误: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 中的应用程序的最佳起点教程?

process.stdout.alwrite.now(d)
这回答了你的问题了吗? Node.js getaddrinfo ENOTFOUND

K
Karol

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
M
Michael_Scharf

检查像这样的主机文件

##
# 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使更改生效!
B
Ben Kim

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


你添加了什么主机名?
@PerStröm 127.0.0.1 本地主机
J
Jay Wick

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


在 OSX sierra 上也有类似的工作,我尝试使用 MongoClient.connect('mongodb://localhost:27017/meanhotel', function(err, db){......} 'mongodb://127.0.0.1:27017/meanhotel', function(err, db){......}
N
Naren Chejara

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

希望对你有帮助 -


A
Adiii

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

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

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


T
T3rm1

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

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

127.0.0.1 tenant1.localhost

E
Er.Subhendu Kumar Pati
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%)

D
Dr NVS

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

系统变量 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

P
Popescu M

我有一个类似的问题,结果问题出在我的代理密码上。

错误是:

“启动 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! network 这是与网络连接有关的问题。npm ERR!network 在大多数情况下,您使用代理或网络设置错误。npm ERR!network npm ERR!network 如果您使用代理,请确保 npm ERR ! 网络“代理”配置设置正确。请参阅:“npm 帮助配置”

原来有趣的部分是这个:

失败,原因:getaddrinfo ENOTFOUND myUsername

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


H
Hady

我遇到了同样的问题,经过反复试验发现我的 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 使用端口 1 的 ICMP。但是您可以 ping localhost。

关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅