user2879041
user2879041

Reputation: 1095

Get response of url in nodejs (express/http)

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.

Upvotes: 4

Views: 15979

Answers (3)

Brian Noah
Brian Noah

Reputation: 2972

I prefer to use the request npm module for http requests. You get a standard callback.

var request = require('request');
opts = {
    url : 'https://www.google.com/pretend/this/exists.xml'
};
request.get(opts, function (error, response, body) {
    //Handle error, and body
});

request.post, request.put, request.head etc.

This allows for a fairly standard handling of errors.

Upvotes: 0

user3998237
user3998237

Reputation:

First you should require the http or https module, depending what you want use:

var https = require('https');
var http = require('http');

http/https modules is one of the cores of Node.js, so you don't need install with npm install.

And what is missing is listen to errors:

var url = 'https://www.google.com/pretend/this/exists.xml';

var options = {
   host: url.split('.com/')[0] + '.com',
   path: '/' + url.split('.com/')[1]
}; 

var req = https.request(options, function (res) {
  var str = ""; 

  res.on('data', function (chunk) {
    str += chunk;
  });

  res.on('end', function () {
    console.log(str);
  });
}); 

req.on('error', function (err) {
     console.log('Error message: ' + err);
});     

req.end();

I also update your code to a better version and clarify version.

Upvotes: 2

Patrick Roberts
Patrick Roberts

Reputation: 51946

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();

Upvotes: 6

Related Questions