title: Downloading PDFs by using node.js results in broken files
body: <p>I am trying to download a number of PDF files from list (var urls) which contains the URLs. The download itself works but the resutlting PDF files are broken, meaning I can save them in my folder but I cannot open them. Is there anyone who has an idea?
  Here is the code:</p>
  
  <pre><code>var urls=require('./paperURLs.json');
  DOWNLOAD_DIR = './paper/';
  
  function readFile(callback) {
    if(urls.length &gt; 0) {
       var setFile = urls.shift(),
       file_name = url.parse(setFile).pathname.split('/').pop(),
       trial = setFile.split('/').pop(),
       file = fs.createWriteStream(DOWNLOAD_DIR + trial);
       http.get(setFile, function(res){
           res.on('error', function(err){
              console.log(err);
          });
          res.on('data', function(data){
              file.write(data);
              console.log(setFile + ' started');
          });
          res.on('end', function(){ 
              console.log(setFile + ' completed, moving on');
              readFile(callback);
          });
      });
    } 
  }
  
   readFile();
  </code></pre>
  
  <p>Thanks in advance for your help.</p>