Chris Mendez in Javascript, For Developers

Create a File Downloader using NodeJS

I wanted to download 100 files but I did not want to do it manually so I created a simple script instead.

var files = [  
"http://www.patatap.com/assets/a/wipe.mp3", "http://www.patatap.com/assets/b/wipe.mp3",  "http://www.patatap.com/assets/c/wipe.mp3",  "http://www.patatap.com/assets/d/wipe.mp3",  "http://www.patatap.com/assets/e/wipe.mp3",  "http://www.patatap.com/assets/f/wipe.mp3",  "http://www.patatap.com/assets/a/veil.mp3",  "http://www.patatap.com/assets/b/veil.mp3",  "http://www.patatap.com/assets/c/veil.mp3",  "http://www.patatap.com/assets/d/veil.mp3",  "http://www.patatap.com/assets/e/veil.mp3",  "http://www.patatap.com/assets/f/veil.mp3",  "http://www.patatap.com/assets/a/prism-1.mp3",  "http://www.patatap.com/assets/b/prism-1.mp3",  "http://www.patatap.com/assets/c/prism-1.mp3",  "http://www.patatap.com/assets/d/prism-1.mp3",  "http://www.patatap.com/assets/e/prism-1.mp3",  "http://www.patatap.com/assets/f/prism-1.mp3",  "http://www.patatap.com/assets/a/prism-2.mp3",  "http://www.patatap.com/assets/b/prism-2.mp3",  "http://www.patatap.com/assets/c/prism-2.mp3",  "http://www.patatap.com/assets/d/prism-2.mp3",  "http://www.patatap.com/assets/e/prism-2.mp3",  "http://www.patatap.com/assets/f/prism-2.mp3",  "http://www.patatap.com/assets/a/prism-3.mp3",  "http://www.patatap.com/assets/b/prism-3.mp3",  "http://www.patatap.com/assets/c/prism-3.mp3",  "http://www.patatap.com/assets/d/prism-3.mp3",  "http://www.patatap.com/assets/e/prism-3.mp3",  "http://www.patatap.com/assets/f/prism-3.mp3",  "http://www.patatap.com/assets/a/clay.mp3",  "http://www.patatap.com/assets/b/clay.mp3",  "http://www.patatap.com/assets/c/clay.mp3",  "http://www.patatap.com/assets/d/clay.mp3",  "http://www.patatap.com/assets/e/clay.mp3",  "http://www.patatap.com/assets/f/clay.mp3",  "http://www.patatap.com/assets/a/piston-1.mp3", "http://www.patatap.com/assets/b/piston-1.mp3", "http://www.patatap.com/assets/c/piston-1.mp3", "http://www.patatap.com/assets/d/piston-1.mp3", "http://www.patatap.com/assets/e/piston-1.mp3", "http://www.patatap.com/assets/f/piston-1.mp3", "http://www.patatap.com/assets/a/piston-2.mp3", "http://www.patatap.com/assets/b/piston-2.mp3", "http://www.patatap.com/assets/c/piston-2.mp3", "http://www.patatap.com/assets/d/piston-2.mp3", "http://www.patatap.com/assets/e/piston-2.mp3", "http://www.patatap.com/assets/f/piston-2.mp3", "http://www.patatap.com/assets/a/piston-3.mp3", "http://www.patatap.com/assets/b/piston-3.mp3", "http://www.patatap.com/assets/c/piston-3.mp3", "http://www.patatap.com/assets/d/piston-3.mp3", "http://www.patatap.com/assets/e/piston-3.mp3", "http://www.patatap.com/assets/f/piston-3.mp3", "http://www.patatap.com/assets/a/flash-1.mp3",  "http://www.patatap.com/assets/b/flash-1.mp3",  "http://www.patatap.com/assets/c/flash-1.mp3",  "http://www.patatap.com/assets/d/flash-1.mp3",  "http://www.patatap.com/assets/e/flash-1.mp3",  "http://www.patatap.com/assets/f/flash-1.mp3",  "http://www.patatap.com/assets/a/flash-2.mp3",  "http://www.patatap.com/assets/b/flash-2.mp3",  "http://www.patatap.com/assets/c/flash-2.mp3",  "http://www.patatap.com/assets/d/flash-2.mp3",  "http://www.patatap.com/assets/e/flash-2.mp3",  "http://www.patatap.com/assets/f/flash-2.mp3",  "http://www.patatap.com/assets/a/flash-3.mp3",  "http://www.patatap.com/assets/b/flash-3.mp3",  "http://www.patatap.com/assets/c/flash-3.mp3",  "http://www.patatap.com/assets/d/flash-3.mp3",  "http://www.patatap.com/assets/e/flash-3.mp3",  "http://www.patatap.com/assets/f/flash-3.mp3",  "http://www.patatap.com/assets/a/dotted-spiral.mp3",   "http://www.patatap.com/assets/b/dotted-spiral.mp3",   "http://www.patatap.com/assets/c/dotted-spiral.mp3",   "http://www.patatap.com/assets/d/dotted-spiral.mp3",   "http://www.patatap.com/assets/e/dotted-spiral.mp3",   "http://www.patatap.com/assets/f/dotted-spiral.mp3",   "http://www.patatap.com/assets/a/suspension.mp3",  "http://www.patatap.com/assets/b/suspension.mp3",  
"http://www.patatap.com/assets/c/suspension.mp3",  "http://www.patatap.com/assets/d/suspension.mp3",  "http://www.patatap.com/assets/e/suspension.mp3",  "http://www.patatap.com/assets/f/suspension.mp3",  "http://www.patatap.com/assets/a/confetti.mp3",  "http://www.patatap.com/assets/b/confetti.mp3",  "http://www.patatap.com/assets/c/confetti.mp3",  "http://www.patatap.com/assets/d/confetti.mp3",  "http://www.patatap.com/assets/e/confetti.mp3",  "http://www.patatap.com/assets/f/confetti.mp3",  "http://www.patatap.com/assets/a/timer.mp3",  "http://www.patatap.com/assets/b/timer.mp3",  "http://www.patatap.com/assets/c/timer.mp3",  "http://www.patatap.com/assets/d/timer.mp3",  "http://www.patatap.com/assets/e/timer.mp3",  "http://www.patatap.com/assets/f/timer.mp3",  "http://www.patatap.com/assets/a/ufo.mp3",  "http://www.patatap.com/assets/b/ufo.mp3",  "http://www.patatap.com/assets/c/ufo.mp3",  "http://www.patatap.com/assets/d/ufo.mp3",  "http://www.patatap.com/assets/e/ufo.mp3",  "http://www.patatap.com/assets/f/ufo.mp3",  "http://www.patatap.com/assets/a/splits.mp3", "http://www.patatap.com/assets/b/splits.mp3",  "http://www.patatap.com/assets/c/splits.mp3",  "http://www.patatap.com/assets/d/splits.mp3",  "http://www.patatap.com/assets/e/splits.mp3",  "http://www.patatap.com/assets/f/splits.mp3",  "http://www.patatap.com/assets/a/moon.mp3",  "http://www.patatap.com/assets/b/moon.mp3",  "http://www.patatap.com/assets/c/moon.mp3",  "http://www.patatap.com/assets/d/moon.mp3",  "http://www.patatap.com/assets/e/moon.mp3",  "http://www.patatap.com/assets/f/moon.mp3",  "http://www.patatap.com/assets/a/strike.mp3",  "http://www.patatap.com/assets/b/strike.mp3",  "http://www.patatap.com/assets/c/strike.mp3",  "http://www.patatap.com/assets/d/strike.mp3",  "http://www.patatap.com/assets/e/strike.mp3",  "http://www.patatap.com/assets/f/strike.mp3",  "http://www.patatap.com/assets/a/zig-zag.mp3",  "http://www.patatap.com/assets/b/zig-zag.mp3",  "http://www.patatap.com/assets/c/zig-zag.mp3",  "http://www.patatap.com/assets/d/zig-zag.mp3",  "http://www.patatap.com/assets/e/zig-zag.mp3",  "http://www.patatap.com/assets/f/zig-zag.mp3",  "http://www.patatap.com/assets/a/squiggle.mp3", "http://www.patatap.com/assets/b/squiggle.mp3", "http://www.patatap.com/assets/c/squiggle.mp3", "http://www.patatap.com/assets/d/squiggle.mp3", "http://www.patatap.com/assets/e/squiggle.mp3", "http://www.patatap.com/assets/f/squiggle.mp3", "http://www.patatap.com/assets/a/bubbles.mp3",  "http://www.patatap.com/assets/b/bubbles.mp3",  "http://www.patatap.com/assets/c/bubbles.mp3",  "http://www.patatap.com/assets/d/bubbles.mp3",  "http://www.patatap.com/assets/e/bubbles.mp3",  "http://www.patatap.com/assets/f/bubbles.mp3",  "http://www.patatap.com/assets/a/corona.mp3",  "http://www.patatap.com/assets/b/corona.mp3",  "http://www.patatap.com/assets/c/corona.mp3",  "http://www.patatap.com/assets/d/corona.mp3",  "http://www.patatap.com/assets/e/corona.mp3",  "http://www.patatap.com/assets/f/corona.mp3",  "http://www.patatap.com/assets/a/pinwheel.mp3", "http://www.patatap.com/assets/b/pinwheel.mp3", "http://www.patatap.com/assets/c/pinwheel.mp3", "http://www.patatap.com/assets/d/pinwheel.mp3", "http://www.patatap.com/assets/e/pinwheel.mp3", "http://www.patatap.com/assets/f/pinwheel.mp3", "http://www.patatap.com/assets/a/glimmer.mp3",  "http://www.patatap.com/assets/b/glimmer.mp3",  "http://www.patatap.com/assets/c/glimmer.mp3",  "http://www.patatap.com/assets/d/glimmer.mp3",  "http://www.patatap.com/assets/e/glimmer.mp3",  "http://www.patatap.com/assets/f/glimmer.mp3"
];
var http = require('http');  
var fs = require('fs');

var download = function(url, dest, cb) {  
  var file = fs.createWriteStream(dest);
  var request = http.get(url, function(response) {
    response.pipe(file);
    file.on('finish', function() {
      file.close(cb);  // close() is async, call cb after close completes.
    });
  });
}


for( var i = 0; i < files.length; i++){  
    var file = files[i];
    var rawFile = file.substr(30);
  download(file, "./public/assets/" + rawFile, function(e){
      console.log("Blah", e );
  } );
}