nodejs-file-downloader
Advanced tools
Comparing version 4.1.0 to 4.1.1
{ | ||
"name": "nodejs-file-downloader", | ||
"version": "4.1.0", | ||
"version": "4.1.1", | ||
"description": "A file downloader for NodeJs", | ||
@@ -5,0 +5,0 @@ "main": "Downloader.js", |
@@ -18,2 +18,3 @@ nodejs-file-downloader is a simple utility for downloading files. It hides the complexity of dealing with streams, redirects, paths and duplicate file names. Can automatically repeat failed downloads. | ||
* [Repeat failed downloads automatically](#repeat-failed-downloads-automatically) | ||
- [Error handling](#error-handling) | ||
@@ -30,11 +31,16 @@ ## Examples | ||
const downloader = new Downloader({ | ||
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. | ||
try { | ||
await downloader.download();//Downloader.download() returns a promise. | ||
console.log('All done'); | ||
console.log('All done'); | ||
} catch (error) {//IMPORTANT: Handle a possible error. An error is thrown in case of network errors, or status codes of 400 and above. | ||
//Note that if the maxAttempts is set to higher than 1, the error is thrown only if all attempts fail. | ||
console.log('Download failed',error) | ||
} | ||
})(); | ||
@@ -53,13 +59,18 @@ | ||
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. | ||
onProgress:function(percentage){//Gets called with each chunk. | ||
console.log('% ',percentage) | ||
} | ||
}) | ||
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. | ||
onProgress:function(percentage){//Gets called with each chunk. | ||
console.log('% ',percentage) | ||
} | ||
}) | ||
await downloader.download(); | ||
try { | ||
await downloader.download(); | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
})(); | ||
})(); | ||
@@ -107,22 +118,25 @@ ``` | ||
```javascript | ||
//The response object is a node response(http.IncomingMessage) | ||
function onResponse(response){ | ||
//Now you can do something with the response, like check the headers | ||
if(response.headers['content-length'] > 1000000){ | ||
console.log('File is too big!') | ||
return false;//If you return false, the download process is stopped, and downloader.download() is resolved. | ||
} | ||
//Returning any other value, including undefined, will tell the downloader to proceed as usual. | ||
//The response object is a node response(http.IncomingMessage) | ||
function onResponse(response) { | ||
//Now you can do something with the response, like check the headers | ||
if (response.headers['content-length'] > 1000000) { | ||
console.log('File is too big!') | ||
return false;//If you return false, the download process is stopped, and downloader.download() is resolved. | ||
} | ||
const downloader = new Downloader({ | ||
url: 'http://212.183.159.230/200MB.zip', | ||
directory: "./", | ||
onResponse | ||
}) | ||
//Returning any other value, including undefined, will tell the downloader to proceed as usual. | ||
} | ||
const downloader = new Downloader({ | ||
url: 'http://212.183.159.230/200MB.zip', | ||
directory: "./", | ||
onResponse | ||
}) | ||
try { | ||
await downloader.download() | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
``` | ||
@@ -161,1 +175,8 @@ | ||
| ||
## Error handling | ||
downloader.download() should be put within a try-catch block. The program will throw an error, just like Axios, in case of network problems | ||
or an http status code of 400 or higher. | ||
If the auto-repeat feature is enabled(by setting the maxAttempts to higher than 1), then only a failure of the final attempt will throw an error. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
155361
177