Introduction to Progress Stream
The progress-stream library is an essential tool for developers who need to monitor the progress of data stream operations. Whether you’re handling file uploads, downloads, or any streaming data processing, this powerful library provides you with detailed progress information to enhance your application’s efficiency.
Getting Started
To get started with progress-stream, you need to install the library using npm:
npm install progress-stream
Once installed, you can require and use it in your Node.js application:
const progress = require('progress-stream');
const fs = require('fs');
const stat = fs.statSync('file.txt');
const str = progress({ length: stat.size, time: 100 });
str.on('progress', function(progress) {
console.log(progress);
});
fs.createReadStream('file.txt')
.pipe(str)
.pipe(fs.createWriteStream('file.copy.txt'));
API Examples
Basic Usage
The basic usage involves creating a progress stream and listening to the ‘progress’ event. Here is a simple example:
const progress = require('progress-stream');
const fs = require('fs');
const stat = fs.statSync('file.txt');
const str = progress({ length: stat.size, time: 100 });
str.on('progress', function(progress) {
console.log('progress:', progress);
});
fs.createReadStream('file.txt')
.pipe(str)
.pipe(fs.createWriteStream('file.copy.txt'));
Custom Configuration
The configuration object can include various options such as length, time, speed, and drain. Here’s how you can customize the progress stream:
const progress = require('progress-stream');
const fs = require('fs');
const options = {
length: 12000,
time: 200,
speed: 2048,
drain: false
};
const str = progress(options);
str.on('progress', function(progress) {
console.log('Custom Progress:', progress);
});
fs.createReadStream('largefile.txt')
.pipe(str)
.pipe(fs.createWriteStream('largefile.copy.txt'));
Using with HTTP Requests
You can also use progress-stream with HTTP requests. Here’s an example of tracking the progress of a file download:
const progress = require('progress-stream');
const request = require('request');
const fs = require('fs');
const url = 'https://example.com/file.zip';
const str = progress();
str.on('progress', function(progress) {
console.log('Download Progress:', progress);
});
request(url)
.pipe(str)
.pipe(fs.createWriteStream('downloadedfile.zip'));
Application Example
Here’s a comprehensive application example that demonstrates using progress-stream for both file upload and download operations, providing users with real-time progress updates:
const express = require('express');
const progress = require('progress-stream');
const multer = require('multer');
const fs = require('fs');
const request = require('request');
const app = express();
const upload = multer({ dest: 'uploads/' });
app.post('/upload', upload.single('file'), (req, res) => {
const file = req.file;
if (!file) {
return res.status(400).send('No file uploaded.');
}
const stat = fs.statSync(file.path);
const str = progress({ length: stat.size, time: 100 });
str.on('progress', function(progress) {
console.log('Upload Progress:', progress);
});
fs.createReadStream(file.path)
.pipe(str)
.pipe(fs.createWriteStream(`uploads/${file.originalname}`))
.on('close', () => {
res.send('File uploaded and processed successfully.');
});
});
app.get('/download', (req, res) => {
const url = 'https://example.com/largefile.zip';
const str = progress();
str.on('progress', function(progress) {
console.log('Download Progress:', progress);
});
res.setHeader('Content-Disposition', 'attachment; filename=largefile.zip');
request(url)
.pipe(str)
.pipe(res);
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});
Using the above application, you can easily upload files to the server and monitor their progress. Additionally, you can download files while tracking the download progress in real-time.
Implementing progress-stream in your Node.js applications is an excellent way to enhance user experience through real-time feedback on data transfer processes.
Hash: c85ada543f77d4da202bb648d9a9a3cbfa4e8bbad63dd20a3b9cd1381fce05a3