简体   繁体   中英

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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM