I want to be able to do a http.get request through node within my own vpn, but it seems likes the function is passing the website to a dns instead of look in my vpn. This following code works with google as the host.
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 );
});
});
but when I change host to a_site_on_my_vpn.com it throws a Domain name not found error, but when i type a_site_on_my_vpn.com on firefox I am able to load the page. I guess one way I can fix this is to find out the host name’s ip but is there an easier way? 😀
I resolve my own answer, but to answer the question in the follow up, the answer is yes here is a snippet that shows that, by doing an DNS ip lookup then going to google by 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 );
});
});
});
And this is the output
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
httpdoes resolve addresses correctly that are in a VPN, but I just had a small typo.