異常getaddrinfo enotfound


在看NodeJS開發指南這本書時,書中的一個例子,講解http.request的.代碼如下:

 1 var http = require('http');
 2 var querystring = require('querystring');
 3 var contents = querystring.stringify({
 4 name: 'byvoid',
 5 email: 'byvoid@byvoid.com',
 6 address: 'Zijing 2#, Tsinghua University',
 7 });
 8 var options = {
 9 host: 'www.byvoid.com',
10 path: '/application/node/post.php',
11 method: 'POST',
12 headers: {
13 'Content-Type': 'application/x-www-form-urlencoded',
14 'Content-Length' : contents.length
15 }
16 };
17 var req = http.request(options, function(res) {
18 res.setEncoding('utf8');
19 res.on('data', function (data) {
20 console.log(data);
21 });
22 });
23 req.write(contents);
24 req.end();
View Code

在運行時會出現以下的error:

events.js:72
   
throw er;// Unhandled 'error' event
         
^
Error: getaddrinfo ENOTFOUND
    at errnoException
(dns.js:37:11)
    at
Object.onanswer [as oncomplete](dns.js:124:16) 

 隨后,查看了官方關於http.request的函數說明.

將代碼host: 'www.byvoid.com',修改為 hostname:url.parse( 'www.byvoid.com').hostname.

即可成功運行.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM