JSPM

  • Created
  • Published
  • Downloads 66460
  • Score
    100M100P100Q156646F
  • License ISC

A file downloader for NodeJs

Package Exports

  • nodejs-file-downloader
  • nodejs-file-downloader/FileProcessor.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nodejs-file-downloader) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

nodejs-file-downloader is a simple utility for downloading files. It hides the complexity of dealing with streams, paths and duplicate file names.

If you encounter any bugs or have a question, please don't hesitate to open an issue.

Installation

$ npm install nodejs-file-downloader

Table of Contents

Examples

Basic

Download a large file with default configuration

const Downloader = require('nodejs-file-downloader');

(async () => {//Wrapping the code with an async function, just for the sake of example.

    const downloader = new Downloader({     
      url: 'http://212.183.159.230/200MB.zip',//If the file name already exists, a new file with the name 200MB1.zip is created.     
      directory: "./downloads",//This folder will be created, if it doesn't exist.               
    })
    
    await downloader.download();//Downloader.download() returns a promise.

    console.log('All done');

})();    

 

Get the progress of a download

const Downloader = require('nodejs-file-downloader');

(async () => {

   const downloader = new Downloader({     
      url: 'http://212.183.159.230/200MB.zip',     
      directory: "./downloads/2020/May",//Sub directories will also be automatically created if they do not exist.           
    })

    downloader.on('progress',(percentage)=>{//Downloader is an event emitter. You can register a "progress" event.
        console.log('% ',percentage)
    })
    
    await downloader.download();   

})();    

 

Custom file name

Normally, nodejs-file-downloader "deduces" the file name, from the URL or the response headers. If you want to choose a custom file name, supply a config.fileName property.

  const downloader = new Downloader({     
      url: 'http://212.183.159.230/200MB.zip',     
      directory: "./downloads/2020/May", 
      fileName:'somename.zip'//This will be the file name.        
  }) 

 

Overwrite existing files

By default, nodejs-file-downloader uses config.cloneFiles = true, which means that files with an existing name, will have a number appended to them.

  const downloader = new Downloader({     
      url: 'http://212.183.159.230/200MB.zip',     
      directory: "./",  
      cloneFiles:false//This will cause the downloader to re-write an existing file.   
  }) 

 

Get response and then download

There is an alternative way to using Downloader.download():

  const downloader = new Downloader({     
      url: 'http://212.183.159.230/200MB.zip',     
      directory: "./",        
  }) 

  const response = await downloader.request()//This function just performs the request. The file isn't actually being downloaded yet. It returns an Axios response object. You can refer to their docs for more details.

  //Now you can do something with the response, like check the headers
  if(response.headers['content-length'] > 1000000){
    await downloader.save()
  }else{
    console.log('File is too big!')
  }  

  //Note that Downloader.download() simply combines these two function calls.