异常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