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

即可成功运行.

posted @ 2014-05-20 09:05  robynhan  阅读(18753)  评论(0编辑  收藏  举报