Node.js - Download Multiple Files

Posted on 20th January 2017

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


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

var urlList = [ "",
	"" ]

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

    .on('error', function(err) {console.log(err)} )
    .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


Alamin | September 9, 2019 5:17 AM |

How do I know all files are downloaded while I download random files?

Chris | September 9, 2019 11:25 AM |

the list of files you want to download will be in the urllist array variable.. so you should know which files are being downloaded.

Pankaj | November 14, 2018 10:43 AM |

Will it bypass the browser prompt for saving the file at a location ?