2016-04-06 53 views
0

我得到follwing錯誤:getaddrinfo ENOTFOUND。我嘗試連接的網址是HTTPS上的子網域。NodeJS DNS錯誤:ENOTFOUND

var options = 
    { 
     family: 4, 
     hostname: 'test.printapi.nl', 
     port: 443, 
     path: '/v1/oauth', 
     method: 'POST', 
     requestCert: true, 
     headers: { 
      host: 'https://test.printapi.nl', 
      //'User-Agent': USER_AGENT, 
      'Content-Type': 'application/x-www-form-urlencoded', 
      'Content-Length': Buffer.byteLength(data) 
     } 
    }; 
    options.agent = http.Agent(options); 

    var req = http.request(options, (res) => { 
     console.log('statusCode: ', res.statusCode); 
     console.log('headers: ', res.headers); 

     res.on('data', (d) => { 
     process.stdout.write(d); 
     }); 
    }, data); 
    console.log(data) 
    req.write(data) 
    req.end(); 

    req.on('error',function(e){ 
    console.log("Error: " + e.message); 
    console.log(e.stack); 
}); 

我已經嘗試了很多東西,與路徑,主機名等。當我使用Fiddler我需要設置一個代理,和提琴手將解決test.printapi.nl的主機名,它會驗證。但是當節點需要這樣做時,它找不到正確的DNS記錄。我試過,它確實返回了正確的DNS服務器。我也嘗試了谷歌DNS服務器,並沒有幫助。我是否忽略了需要爲主機名設置的內容?

--edit 如果我使用https我得到cannot read property 'maxCachedSessions'

提前感謝!

回答

3

ENOTFOUND表示找不到hostname的DNS條目。在這種情況下的問題是hostname應該而不是包括路徑。相反,它應該是這樣的:

var options = { 
    hostname: 'test.printapi.nl', 
    path: '/v1/oauth' 
    // ... 

而且,你不必明確設置Host頭做。 port號碼與您使用的模塊不一致 - 如果它是https連接,請使用https.request()而不是http.request()

最後,你可以刪除或將需要改變這一行:

options.agent = http.Agent(options); 

這樣:

options.agent = new https.Agent(options); 
+0

如果我使用https模塊,一個整體的其他一系列問題apear,即不要使用http ...如果我使用https,我不能讀取未定義的屬性'maxCachedSessions'。上面提到的錯誤不會隨着使用路徑而改變,但我會改變我的代碼示例,它會更清晰。我也會發布爲什麼我不使用https的問題,以避免將來的查詢。 –

+0

您使用的是什麼版本的節點? – mscdex

+0

我已經試過4.4.2,現在我正在運行5.10.1,所以LTS和穩定性是最近的。 –