What is request-progress?
The request-progress npm package is used to track the progress of HTTP requests made using the request library. It provides a simple way to monitor the progress of file uploads and downloads, giving you real-time feedback on the status of your requests.
What are request-progress's main functionalities?
Track Download Progress
This feature allows you to track the progress of a file download. The 'progress' event provides real-time updates on the download status, including the percentage completed, speed, and remaining time.
const request = require('request');
const progress = require('request-progress');
progress(request('http://example.com/file.zip'))
.on('progress', function (state) {
console.log('progress', state);
})
.on('error', function (err) {
console.error('error', err);
})
.on('end', function () {
console.log('Download completed');
});
Track Upload Progress
This feature allows you to track the progress of a file upload. The 'progress' event provides real-time updates on the upload status, including the percentage completed, speed, and remaining time.
const fs = require('fs');
const request = require('request');
const progress = require('request-progress');
const fileStream = fs.createReadStream('path/to/file.zip');
progress(request.post('http://example.com/upload', { formData: { file: fileStream } }))
.on('progress', function (state) {
console.log('progress', state);
})
.on('error', function (err) {
console.error('error', err);
})
.on('end', function () {
console.log('Upload completed');
});
Other packages similar to request-progress
progress-stream
The progress-stream package provides a way to monitor the progress of data being streamed. It can be used with any stream, not just HTTP requests, making it more versatile than request-progress. However, it requires more manual setup to integrate with HTTP requests.
axios-progress-bar
The axios-progress-bar package is designed to work with the axios HTTP client. It provides a progress bar for file uploads and downloads, similar to request-progress. It is a good alternative if you are using axios instead of the request library.
request-progress
Tracks the download progress of a request made with request.
Installation
$ npm install request-progress
Usage
var fs = require('fs');
var request = require('request');
var progress = require('request-progress');
progress(request('http://google.com/doodle.png'), {
throttle: 2000,
delay: 1000
})
.on('progress', function (state) {
console.log('total size in bytes', state.total);
console.log('received size in bytes', state.received);
console.log('percent', state.percent);
})
.on('error', function (err) {
})
.pipe(fs.createWriteStream('doodle.png'))
.on('error', function (err) {
})
.on('close', function (err) {
})
Note that the state
object emitted in the progress
event is reused to avoid creating a new object for each event.
License
Released under the MIT License.