title: Download a file from web using Node js and loop
body: <p>I want to download multiple files from the web using this code:</p>
  
  <pre><code>    var fs = require('fs');
      var http = require('http');
      var request = require('request');
  
      var file;
      for(var i = 1; i &lt;= 5; i++) {
           //CHECK IF REMOTE FILE EXISTS
           request('http://webaddress.com/filename' + i + '.jar', function (err, resp) {
  
           //IF EXISTS DO
           if (resp.statusCode == 200) {
               //DOWNLOAD DATA AND CREATE A NEW .JAR FILE
               file = fs.createWriteStream('D:\\filename' + i + '.jar');
               http.get('http://webaddress.com/filename' + i + '.jar', function(response) {
                    response.pipe(file);
                    file.on('finish', function() {
                        file.close();
                    });
               });
           }
  
           //FILE DOES NOT EXIST
           });
       }
  </code></pre>
  
  <p>The result I want is: multiple files downloaded with filenames <em>filename1-5.jar</em>. The result I am getting is just 1 file with filename <em>filename5.jar</em> (or the last value of the i var in the loop). What am I doing wrong?</p>