4

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.
Community
  • 1
  • 1
user2879041
  • 1,077
  • 1
  • 12
  • 25

3 Answers3

6

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();
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
2

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.

  • what did you even change besides the mistake res.ond – user2879041 Jan 29 '15 at 22:11
  • check the stackoverflow question I referenced, I really don't think this solution is going to work – user2879041 Jan 29 '15 at 22:17
  • you must always call req.end() to signify that you're done with the request - even if there is no data being written to the request body. –  Jan 29 '15 at 22:19
  • @MrBearAndBeer he *did* call ``req.end()`` in his post with ``http.request(opt, callback).end();``... – Patrick Roberts Jan 29 '15 at 22:20
  • so, what is missing is listen to errors, like you sad @PatrickRoberts –  Jan 29 '15 at 22:21
  • i was assuming that he just didn't put the require code in the question –  Jan 29 '15 at 22:23
  • there is no difference between "" and '', in fact.. in the official documentation -> http://nodejs.org/api/http.html#http_http_request_options_callback they are using '' :). –  Jan 29 '15 at 22:39
  • Yes I'm aware, but clean code is consistent, so pick one or the other. – Patrick Roberts Jan 29 '15 at 22:40
0

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.

Brian Noah
  • 2,962
  • 18
  • 27
  • updated to a 'get'. I can't infer that it's a simple application from the question. the request module takes all the guesswork from working with chunked data and only does a callback on('end'). – Brian Noah Jan 29 '15 at 22:44