【问题标题】:Node JS: Call external HTTP request by 'https' libraryNode JS:通过“https”库调用外部 HTTP 请求
【发布时间】:2017-02-12 06:30:48
【问题描述】:

当我通过基本的“https”库调用外部 http 请求时,我无法提及端口地址,但是当我显示日志错误时,它显示端口地址为 '80' 为什么会显示以及我的代码中的问题在哪里??

    var http = require("http");
    http.createServer(function (request, response) {
    var options = {
      hostname: 'example.com',
      method: 'GET',
      headers: {
                'Authorization':'Bearer 22bc5ce9f7934da6616ac7d883dbb8c9',
                'Content-Type': 'application/json'
      }
    };

    var req = http.request(options, (res) => {
      res.setEncoding('utf8');
      res.on('data', (chunk) => {
        console.log(`BODY: ${chunk}`);
      });
      res.on('end', () => {
        console.log('No more data in response.');
      });
    });
    req.end();
    req.on('error', (e) => {
      console.log(e);
    });


    // Listen on the 8080 port.
    }).listen(8080);

错误信息:

{ Error: getaddrinfo ENOTFOUND https://example.com:80
    at errnoException (dns.js:28:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:79:26)
  code: 'ENOTFOUND',
  errno: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'https://example.com',
  host: 'https://example.com',
  port: 80 }

【问题讨论】:

    标签: javascript node.js https


    【解决方案1】:

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

    var options = {
          host: 'example',
          method: 'GET',
          headers: {
                    'Authorization':'Bearer 22bc5ce9f7934da6616ac7d883dbb8c9',
                    'Content-Type': 'application/json'
          }
    };
    

    看到这个帖子Here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-24
      • 1970-01-01
      • 1970-01-01
      • 2016-04-08
      • 2018-12-06
      • 2017-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多