【问题标题】:Error: getaddrinfo ENOTFOUND while making get request to localhost, Nodejs错误:getaddrinfo ENOTFOUND 同时向 localhost、Nodejs 发出获取请求
【发布时间】:2017-09-24 17:58:33
【问题描述】:

我正在尝试通过节点 http 模块向本地运行的 etcd 实例发出 het 请求。

代码如下所示

'use strict';
const express = require('express');
const app = express();
var http = require('http');

const port = 10111;

var encoded_url = encodeURI('/v2/keys/message -X GET');

var options = {
  host: 'http://127.0.0.1:2379',
  path: encoded_url
};

var callback = function (response) {
  var str = '';

  //another chunk of data has been recieved, so append it to `str`
  response.on('data', function (chunk) {
    str += chunk;
  });

  //the whole response has been recieved, so we just print it out here
  response.on('end', function () {
    console.log(str);
  });
}

http.request(options, callback).end();

app.listen(port, () => {
  console.log("server started on port " + port);
});

但我收到以下错误

Error: getaddrinfo ENOTFOUND http://127.0.0.1:2379 http://127.0.0.1:2379:80
    at errnoException (dns.js:28:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)

如果我从终端发出相同的 curl 请求,我会得到结果

卷曲http://127.0.0.1:2379/v2/keys/message -X GET

无法找出问题所在。

【问题讨论】:

    标签: node.js etcd etcd3


    【解决方案1】:

    默认http.request()使用端口80

    改用这个:

    var options = {
      protocol: 'http:',
      host: '127.0.0.1',
      port: 2379,
      path: encoded_url
    };
    

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 1970-01-01
      • 2019-01-06
      • 2021-05-10
      • 1970-01-01
      • 2022-11-04
      • 2022-01-18
      • 2021-04-22
      • 1970-01-01
      相关资源
      最近更新 更多