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.
You need to listen to the error
event on the request. If there is no handler attached, it will throw the error, but if there is one attached, it will pass the error as an argument in the asynchronous callback. In addition, you should use the https
module for node, not http
if you intend to make a secure request. So try this:
var https = require("https");
var url = "https://www.google.com/pretend/this/exists.xml";
var opt = {
host: url.split(".com/")[0] + ".com",
path: "/" + url.split(".com/")[1]
};
function callback(response) {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
console.log(str);
});
}
var request = https.request(opt, callback);
request.on("error", function (error) {
console.error(error);
});
request.end();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With