2016-10-04 169 views
0

當我通過基本的'https'庫調用外部http請求時,我不能提到端口地址,但是當我顯示日誌錯誤時,它顯示端口地址是'80'顯示和我的代碼中的問題在哪裏?節點JS:通過'https'庫調用外部HTTP請求

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 } 

回答

0

getaddrinfo ENOTFOUND意味着客戶端無法連接到指定的地址,請嘗試指定主機,而無需HTTP。」試試這個:

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

看到這個職位Here