title: Node.js - read and download all files in directory from server and save locally
body: <p>I have a Node Webkit Desktop App and need to download files from the server and save locally for when users are offline.  I can download and save a file when I know what the file name is, but how do I read the contents of a directory on the server so I can download each file?</p>
  
  <pre><code>function cacheFiles(filelink, filepath, cb) {
      var path_array =  filelink.split("/");
      var foldername =  path_array[path_array.length - 2]
  
  //create new folder for locally html files
  var newdir = filepath + '/' + foldername;
  if (fs.existsSync(newdir)){
      alert('file already exists, cannot cache this file.');
  } else {
      fs.mkdirSync(newdir);
  }
  //download and save index.html - THIS WORKS
  var indexfile = fs.createWriteStream(newdir+'/index.html');
  var request = http.get(filelink, function(response) {
      response.pipe(indexfile);
      indexfile.on('finish', function() {
          indexfile.close(cb);
      });
  });
  //read contents of data folder - THIS DOESN'T WORK
  var datafolder = filelink.replace('index.html','');
  
  fs.readdir( datafolder, function (err, datafiles) { 
      if (!err) {
          console.log(datafiles);
      }else{
          console.log(err) ; 
      }   
  });
  </code></pre>
  
  <p>}</p>
  
  <p>The error I get in my console is:  </p>
  
  <blockquote>
    <p>"ENOENT: no such file or directory, scandir
    'C:\Users\my.name\desktopApp\app\http:\www.mysite.co.uk\wp-content\uploads\wifi_corp2\data'"</p>
  </blockquote>
  
  <p>The above is looking for the files locally and not at the online link I supplied in <strong><em>filelink</em></strong>  eg. 
  <a href="http://www.mysite.co.uk/wp-content/uploads/wifi_corp2/data">http://www.mysite.co.uk/wp-content/uploads/wifi_corp2/data</a></p>