I'm trying to get the response of two URLs in nodejs but there's a problem with http.request. Here's what I have so far:
var url = "https://www.google.com/pretend/this/exists.xml";
var opt = {
host: url.split(".com/")[0] + ".com",
path: "/" + url.split(".com/")[1]
};
callback = function(response){
var str = "";
response.on('data', function(chunk){
str += chunk;
});
response.on('end', function(){
console.log(str);
});
}
http.request(opt, callback).end();
and I'm getting this 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)
so I googled and got this stackoverflow issue nodejs httprequest with data - getting error getaddrinfo ENOENT in which the accepted answer says that you need to leave out the protocol.. but here's the issue, I need to check if
https://www.google.com/pretend/this/exists.xml
gives a 200 and if it doesn't (404) then I need to check if
http://www.google.com/pretend/this/exists.xml
gives a valid response
So that's the issue, I need to check a response by specific protocol.
Any ideas?
edit: Just now looking at the http doc (lazy I know) and I'm seeing http.get example.. I'll try that now
edit 2 :
so I tried this
http.get(url, function(res){
console.log("response: " + res.statusCode);
}).on('error', function(e){
console.log("error: " + e.message);
});
and apparently https is not supported.
Error: Protocol:https: not supported.