6

How can we stop the remaining response from a server - For eg.

http.get(requestOptions, function(response){

//Log the file size;
console.log('File Size:', response.headers['content-length']);

// Some code to download the remaining part of the response?

}).on('error', onError);

I just want to log the file size and not waste my bandwidth in downloading the remaining file. Does nodejs automatically handles this or do I have to write some special code for it?

tusharmath
  • 10,622
  • 12
  • 56
  • 83

2 Answers2

13

If you just want fetch the size of the file, it is best to use HTTP HEAD, which returns only the response headers from the server without the body.

You can make a HEAD request in Node.js like this:

var http = require("http"),
    // make the request over HTTP HEAD
    // which will only return the headers
    requestOpts = {
    host: "www.google.com",
    port: 80,
    path: "/images/srpr/logo4w.png",
    method: "HEAD"
};

var request = http.request(requestOpts, function (response) {
    console.log("Response headers:", response.headers);
    console.log("File size:", response.headers["content-length"]);
});

request.on("error", function (err) {
    console.log(err);
});

// send the request
request.end();

EDIT:

I realized that I didn't really answer your question, which is essentially "How do I terminate a request early in Node.js?". You can terminate any request in the middle of processing by calling response.destroy():

var request = http.get("http://www.google.com/images/srpr/logo4w.png", function (response) {
    console.log("Response headers:", response.headers);

    // terminate request early by calling destroy()
    // this should only fire the data event only once before terminating
    response.destroy();

    response.on("data", function (chunk) {
        console.log("received data chunk:", chunk); 
    });
});

You can test this by commenting out the the destroy() call and observing that in a full request two chunks are returned. Like mentioned elsewhere, however, it is more efficient to simply use HTTP HEAD.

Robert Mitchell
  • 1,334
  • 8
  • 10
  • Thanks, for the answer. How is it different than response.end() and when should I use that? – tusharmath Apr 24 '13 at 16:39
  • One more thing, if I dont bind a listener to the 'data' event will the data still be transferred ? I mean my bandwidth will get wasted unnecessarily? – tusharmath Apr 24 '13 at 16:54
  • Yes, the data will still be sent down to the client even if you don't handle the "data" event. – Robert Mitchell Apr 24 '13 at 20:29
  • Regarding the end() vs. destroy() question, the response object doesn't have an end() in this context. – Robert Mitchell Apr 24 '13 at 20:34
  • This is a follow up question - How do I end the response before getting the response object? – tusharmath Jul 17 '13 at 17:41
  • @RobertMitchell Thanks for posting the answer to the question as well as what he should actually do. This is the 1st SO hit on a Google search for `node cancel http get request` and it would be a shame if it didn't actually answer that question just because it wasn't what should be done in this particular case. – Vala Aug 19 '15 at 11:25
3

You need to perform a HEAD request instead of a get

Taken from this answer

var http = require('http');
var options = {
    method: 'HEAD', 
    host: 'stackoverflow.com', 
    port: 80, 
    path: '/'
};
var req = http.request(options, function(res) {
    console.log(JSON.stringify(res.headers));
    var fileSize = res.headers['content-length']
    console.log(fileSize)
  }
);
req.end();
Community
  • 1
  • 1
Noah
  • 33,851
  • 5
  • 37
  • 32