http.get通过vpn +节点

我希望能够通过我自己的vpn节点做一个http.get请求,但似乎喜欢的function是传递网站到DNS,而不是看我的VPN。 以下代码与谷歌作为主机。

http = require('http'); var host = "www.google.com"; var options = {host:host, port:80, path:'/'} http.get( options, function( res ){ res.setEncoding('utf8'); res.on( 'data', function( chunk ){ console.log( chunk ); }); }); 

但是当我更改主机到a_site_on_my_vpn.com它会引发域名未find错误,但是当我在Firefox上键入a_site_on_my_vpn.com我能够加载页面。 我想我可以解决这个问题的一个方法是找出主机名的IP,但有没有更简单的方法? :d

我解决了我自己的答案,但要回答在后续的问题,答案是肯定的这里是一个片段,表明,通过做一个DNS IP查找,然后去IP谷歌。

 var dns = require('dns'); var http = require('http'); dns.resolve4('www.google.com', function (err, addresses) { if (err) throw err; console.log('addresses: ' + JSON.stringify(addresses)); var options = { host: addresses[0] } http.get(options, function(res){ console.log("Sending http request to google with options", options); var htmlsize = 0; res.on('data', function( chunk ){ htmlsize+=chunk.length; }); res.on('end', function(){ console.log( "got html of lenght", htmlsize ); }); }); }); 

这是输出

 addresses: ["74.125.224.82","74.125.224.81","74.125.224.84","74.125.224.80","74.125.224.83"] Sending http request to google with options { host: '74.125.224.82' } got html of lenght 33431 

http确实正确地parsing了VPN中的地址,但我只是有一个小的错字。