Uploading series of large files in node to a web API
<p>I am trying to create a node script to traverse a folder of large files (mp3s containing 1 hour music sets) and upload them to Mixcloud via their API. It works except in the case where it hits the API rate limit and needs to wait x seconds - I set a timeout and loop back to beginning, but after the set amount of time, the process exits and doesn't log any useful output. Is this due to a bug in my code, or the way I'm running it? I'm currently running it with <code>sudo nohup node index.js &gt; log.log</code>, is this incorrect? my end goal is a script that can be run at the end of every day of our radio station and upload the archived files.</p>

<pre><code>const restler = require('restler');
const fs = require('fs');
const readDir = require('readdir');
const powerOff = require('power-off');

const options = {
  folder: 'files',
  completefolder: 'complete',
  accesstoken: 'xxxxxxxxxxxxxxxxxxxxxxx'
}

// shut down computer
const shutDown = () =&gt; {powerOff((err, stderr, stdout) =&gt; {
      if(!err &amp;&amp; !stderr) {
          console.log(stdout);
      }
  })
};

// uploadFile uploads file with restler to mixcloud, if api returns rate limiting object, try again in x seconds
const uploadFile = (folder, filename) =&gt; {
  const filepath = `./${folder}/${filename}`
  fs.stat(`./${folder}/${filename}`, function(err, stats) {
      const size = stats.size;
      restler.post(`https://api.mixcloud.com/upload/?access_token=${options.accesstoken}`, {
          multipart: true,
          data: {
              "mp3": restler.file(`./${folder}/${filename}`, null, size, null, 'audio/mpeg'),
              "name": filename,
              // "unlisted": true

              // more data can be added here depending on changes in workflow, automate images etc
          }
      }).on("complete", function(data) {
          const returned = JSON.parse(data);
          if (returned.error) {
            if (returned.error.type == "RateLimitException") {
              // try again in x seconds
              console.log(`uploading too fast, retrying upload of ${filename}after ${returned.error.retry_after} seconds`);
              setTimeout(() =&gt; {(folder, filename) =&gt; uploadFile}, returned.error.retry_after*1000);
            }
            else {
              console.log('non-rate-limiting error');
              console.log(returned);
            }
          }
          else {
            console.log('Success!');
            console.log(returned);
            // move uploaded files into completed folder
            fs.rename(`./${folder}/${filename}`, `./${options.completefolder}/${filename}`, (err)=&gt; {
              if (err) {
                console.log(err)
              }
              else {
                counter += 1;
                console.log(counter);
                if (counter === files.length) {
                  console.log('done');
                  shutDown();
                }
              }
            })
          }
      });
  });
};

// get all mp3s and upload all of them
const files = readDir.readSync(`./${options.folder}`, ['**.mp3'] );
let counter = 0;
for (var i = 0; i &lt; files.length; i++) {
  uploadFile(options.folder, files[i])
};
</code></pre>