YouTip LogoYouTip

Nodejs Stream Module

Node.js stream Module | \\\\n\\\\nJava File[Node.js Built-in Modules](#)\\\\n\\\\n* * *\\\\n\\\\nStream is an abstract interface for handling streaming data in Node.js. You can imagine a stream as flowing water, with data flowing from one place to another like water. This approach is particularly suitable for handling large files or continuous data sources because it doesn't need to load all data into memory at once.\\\\n\\\\n### Why do we need Stream?\\\\n\\\\n1. **Memory efficiency**: When processing large files, there's no need to load the entire file into memory at once\\\\n2. **Time efficiency**: Data can be processed as it arrives, without waiting for all data to be ready\\\\n3. **Composability**: Multiple streams can be connected like pipes to form a data processing pipeline\\\\n\\\\n* * *\\\\n\\\\n## Four Basic Types of Stream\\\\n\\\\n### 1. Readable Stream\\\\n\\\\nA readable stream is a source of data from which data can be read. For example, reading content from a file or fetching data from an HTTP request.\\\\n\\\\n## Example\\\\n\\\\n```javascript\\\\nconst fs = require('fs');\\\\n\\\\nconst readableStream = fs.createReadStream('example.txt');\\\\n\\\\nreadableStream.on('data',(chunk)=>{\\\\n\\\\n console.log(`Received ${chunk.length} bytes of data`);\\\\n\\\\n});\\\\n\\\\nreadableStream.on('end',()=>{\\\\n\\\\n console.log('No more data');\\\\n\\\\n});\\\\n\\\\n### 2. Writable Stream\\\\n\\\\nA writable stream is a destination for data, to which data can be written. For example, writing to a file or sending an HTTP response.\\\\n\\\\n## Example\\\\n\\\\n```javascript\\\\nconst fs = require('fs');\\\\n\\\\nconst writableStream = fs.createWriteStream('output.txt');\\\\n\\\\nwritableStream.write('First line of data n');\\\\n\\\\n writableStream.write('Second line of data n');\\\\n\\\\n writableStream.end('Last line of data');\\\\n\\\\n### 3. Duplex Stream\\\\n\\\\nA duplex stream is both readable and writable, such as a TCP socket.\\\\n\\\\n## Example\\\\n\\\\n```javascript\\\\nconst{ Duplex }= require('stream');\\\\n\\\\nconst myDuplex =new Duplex({\\\\n\\\\n write(chunk, encoding, callback){\\\\n\\\\n console.log(chunk.toString());\\\\n\\\\n callback();\\\\n\\\\n},\\\\n\\\\nread(size){\\\\n\\\\nthis.push('Data read from duplex stream');\\\\n\\\\nthis.push(null);// Indicates end of data\\\\n\\\\n}\\\\n\\\\n});\\\\n\\\\n### 4. Transform Stream\\\\n\\\\nA transform stream is a special kind of duplex stream that can modify or transform data during reading and writing. For example, compressing or encrypting data.\\\\n\\\\n## Example\\\\n\\\\n```javascript\\\\nconst{ Transform }= require('stream');\\\\n\\\\nconst upperCaseTr =new Transform({\\\\n\\\\n transform(chunk, encoding, callback){\\\\n\\\\nthis.push(chunk.toString().toUpperCase());\\\\n\\\\n callback();\\\\n\\\\n}\\\\n\\\\n});\\\\n\\\\nprocess.stdin.pipe(upperCaseTr).pipe(process.stdout);\\\\n\\\\n* * *\\\\n\\\\n## Common Methods of Stream\\\\n\\\\n### pipe() Method\\\\n\\\\nThe `pipe()` method is one of the most powerful features of Stream, which can connect multiple streams together to form a pipeline.\\\\n\\\\n## Example\\\\n\\\\n```javascript\\\\nconst fs = require('fs');\\\\n\\\\n// Create a readable stream and a writable stream\\\\n\\\\nconst readStream = fs.createReadStream('input.txt');\\\\n\\\\nconst writeStream = fs.createWriteStream('output.txt');\\\\n\\\\n// Use pipe Connect the readable stream and writable stream\\\\n\\\\n readStream.pipe(writeStream);\\\\n\\\\nwriteStream.on('finish',()=>{\\\\n\\\\n console.log('Data writing completed');\\\\n\\\\n});\\\\n\\\\n### Event Listening\\\\n\\\\nStream is an instance of EventEmitter, and you can handle various situations by listening to events:\\\\n\\\\n* `data` - Fired when there is data available to read\\\\n* `end` - Fired when there is no more data to read\\\\n* `error` - Fired when an error occurs\\\\n* `finish` - Fired when all data has been flushed to the underlying system\\\\n\\\\n* * *\\\\n\\\\n## Practical Application Examples\\\\n\\\\n### Example 1: File Copying\\\\n\\\\n## Example\\\\n\\\\n```javascript\\\\nconst fs = require('fs');\\\\n\\\\nfunction copyFile(source, target, cb){\\\\n\\\\n// Create read-write stream\\\\n\\\\nconst rd = fs.createReadStream(source);\\\\n\\\\nconst wr = fs.createWriteStream(target);\\\\n\\\\n// Handle errors\\\\n\\\\n rd.on('error', err => cb(err));\\\\n\\\\n wr.on('error', err => cb(err));\\\\n\\\\n// Completion callback\\\\n\\\\n wr.on('finish', cb);\\\\n\\\\n// Start copying\\\\n\\\\n rd.pipe(wr);\\\\n\\\\n}\\\\n\\\\n// Use\\\\n\\\\n copyFile('source.txt','target.txt', err =>{\\\\n\\\\nif(err) console.error(err);\\\\n\\\\nelse console.log('Copy completed');\\\\n\\\\n});\\\\n\\\\n### Example 2: HTTP Server Compressing Files\\\\n\\\\n## Example\\\\n\\\\n```javascript\\\\nconst fs = require('fs');\\\\n\\\\nconst http = require('http');\\\\n\\\\nconst zlib = require('zlib');\\\\n\\\\nhttp.createServer((req, res)=>{\\\\n\\\\n// Create a readable stream\\\\n\\\\nconst readStream = fs.createReadStream('./largeFile.txt');\\\\n\\\\n// Set ResponseHeader\\\\n\\\\n res.writeHead(200,{\\\\n\\\\n'Content-Type':'text/plain',\\\\n\\\\n'Content-Encoding':'gzip'\\\\n\\\\n});\\\\n\\\\n// Pipe chain: Read -> Compression -> Response\\\\n\\\\n readStream.pipe(zlib.createGzip()).pipe(res);\\\\n\\\\n}).listen(3000);\\\\n\\\\n* * *\\\\n\\\\n## Best Practices for Stream\\\\n\\\\n1. **Error handling**: Always add error handling listeners to streams\\\\n2. **Memory management**: Use streams instead of reading all at once for large files\\\\n3. **Pipe chaining**: Use the pipe() method reasonably to combine multiple streams\\\\n4. **Backpressure handling**: Handle backpressure appropriately when write speed cannot keep up with read speed\\\\n\\\\n### Error Handling Example\\\\n\\\\n## Example\\\\n\\\\n```javascript\\\\nconst fs = require('fs');\\\\n\\\\nconst readStream = fs.createReadStream('not-exist-file.txt');\\\\n\\\\nreadStream.on('error',(err)=>{\\\\n\\\\n console.error('Error occurred:', err.message);\\\\n\\\\n});\\\\n\\\\nBy mastering the Stream module in Node.js, you can efficiently handle various I/O operations. Especially when dealing with large files or real-time data, streams will become a powerful tool in your arsenal.\\\\n\\\\nJava File[Node.js Built-in Modules](#)
← Nodejs Os ModuleNodejs Querystring Module β†’