Reading from Streams
NodeJS provides various readable streams, such as fs.createReadStream()
for reading data from files, http.IncomingMessage
for reading data from HTTP requests, and process.stdin
for reading data from standard input.
const fs = require('fs');
const readStream = fs.createReadStream('input.txt');
readStream.on('data', (chunk) => {
console.log('Received data chunk:', chunk.toString());
});
readStream.on('end', () => {
console.log('Finished reading data.');
});
How to handle streaming data in Node ?
Streaming data in NodeJS involves processing data in chunks as it becomes available, rather than waiting for the entire dataset to be loaded into memory. This approach is particularly useful for handling large files, network data, or real-time data sources. NodeJS provides a powerful set of streaming APIs that allow developers to efficiently handle streaming data. Here’s how you can handle streaming data in NodeJS:
Contact Us