title: How to concurrent download files using cheerio and nodejs?
body: <p>I have a website with multiple pages, each page lists download links which I want to scrap and download.
  I have few issues with it:</p>
  
  <ul>
  <li>My script only downloads about 4-5 files and getting stuck.</li>
  <li>I would like to concurrently download as much files as my CPU can.</li>
  <li>I got stuck with maximum event emitters, I don't understand why is that so I just go</li>
  <li>How to follow redirects purely using request module (without follow-redirects)?</li>
  <li>How to download the file like the browser does without mentioning it's name? there is no <code>content-disposition</code> but I think the browser follow redirects and the redirected URL has the filename in it's path.</li>
  </ul>
  
  <p>My current code looks like so:</p>
  
  <pre><code>var request = require('request');
  var cheerio = require('cheerio');
  var https = require('follow-redirects').https;
  require('events').EventEmitter.prototype._maxListeners = 1000;
  
  for(var i = 1; i &lt;= 10000; i++) {
      (function(i){
          url = 'http://mywebsite.com/files?page=' + i;
          request(url, gotHTML)
      })(i);
  }
  
  function gotHTML(err, resp, html) {
      var $ = cheerio.load(html);
      $('.file-header').each(function() {
          var data = $(this);
          var fileLink = data.children().first().children().first().attr('href');
          var fileName = fileLink.substring(10);
          var downloadLink = 'https://mywebsite.com/api/download/' + fileName;
          download(downloadLink, function() {
              console.log('downloaded');
          })
      })
  }
  
  function download(url, cb) {
    var request = https.get(url, function(response) {
      var location = request.res.headers.location;
      console.log(location);
      location = location.split('/').pop();
      console.log(location);
      var file = fs.createWriteStream(location);
      response.pipe(file);
      file.on('finish', function() {
        file.close(cb);
      });
    });
  }
  </code></pre>