簡體   English   中英

節點JS:通過“ https”庫調用外部HTTP請求

[英]Node JS: Call external HTTP request by 'https' library

當我通過基本的'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 }

getaddrinfo ENOTFOUND表示客戶端無法連接到給定的地址。請嘗試指定不帶http的主機。” 嘗試這個:

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

在這里看到這個帖子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM