Node.js - Download Multiple Files

This code uses the native module fs as well an add-on module - request. The request module allows you to make HTTP requests (GET, POST, PUT, PATCH, DELETE) and interact with RESTful APIs easily. To install request

npm install request

DownloadFiles.js

var fs = require('fs');
var request = require('request');

var urlList = [ "http://cdn.mos.musicradar.com/audio/samples/musicradar-western-guitar-samples.zip",
	"http://cdn.mos.musicradar.com/audio/samples/musicradar-wavedrum-samples.zip" ]

var download = function(url, dest, callback){

    request.get(url)
    .on('error', function(err) {console.log(err)} )
    .pipe(fs.createWriteStream(dest))
    .on('close', callback);

};

urlList.forEach( function(str) {
	var filename =  str.split('/').pop();
	console.log('Downloading ' + filename);
	download(str, filename, function(){console.log('Finished Downloading' + filename)});
});

The download function takes three arguments - url, destination filename and callback function. The response of GET request is piped to a file stream. The callback is executed on close event. Download URLs are in an array called urlList. The download function is called for each url in the urlList. The destination filename is stripped out of the url by splitting at ('/') and popping the last part. The callback function just logs the filename when the download finishes.


Post a comment

Name

Your Comment

Email (We dont publish it)

Comments

Nothing yet..be the first to share wisdom.