title: Download large file with node.js avoiding high memory consumption
body: <p>I`m trying to create a file downloader as a background service but when a large file is scheduled, it's first put in memory and then, at the end of the download the file is written to disk. </p>
  
  <p>How can I make the file be wrote gradually to the disk preserving memory considering that I may have lots of files being downloaded at the same time?</p>
  
  <p>Here's the code I`m using:</p>
  
  <pre><code>var sys = require("sys"),
      http = require("http"),
      url = require("url"),
      path = require("path"),
      fs = require("fs"),
      events = require("events");
  
  var downloadfile = "http://nodejs.org/dist/node-v0.2.6.tar.gz";
  
  var host = url.parse(downloadfile).hostname
  var filename = url.parse(downloadfile).pathname.split("/").pop()
  
  var theurl = http.createClient(80, host);
  var requestUrl = downloadfile;
  sys.puts("Downloading file: " + filename);
  sys.puts("Before download request");
  var request = theurl.request('GET', requestUrl, {"host": host});
  request.end();
  
  var dlprogress = 0;
  
  
  setInterval(function () {
     sys.puts("Download progress: " + dlprogress + " bytes");
  }, 1000);
  
  
  request.addListener('response', function (response) {
      response.setEncoding('binary')
      sys.puts("File size: " + response.headers['content-length'] + " bytes.")
      var body = '';
      response.addListener('data', function (chunk) {
          dlprogress += chunk.length;
          body += chunk;
      });
      response.addListener("end", function() {
          fs.writeFileSync(filename, body, 'binary');
          sys.puts("After download finished");
      });
  
  });
  </code></pre>