Nodejs Stream
Node.js Stream is an abstract interface for processing streaming data, widely used in scenarios such as file operations and network communication.\\n\\nA major advantage of streaming data processing is that you can start processing data during transmission without waiting for the entire data to be loaded. This allows Node.js to efficiently process large amounts of data without consuming too much memory.\\n\\n!(#)\\n\\nStream is an abstract interface, and many objects in Node implement this interface. For example, the request object initiating a request to an HTTP server is a Stream, as is stdout (standard output).\\n\\nNode.js Stream is a way of processing data streams, which allows you to process data in a streaming form rather than loading all the data into memory at once. This is very useful for processing large amounts of data or achieving efficient data transmission.\\n\\nIn Node.js, there are four types of streams:\\n\\n* **Readable** - Readable operation.\\n\\n* **Writable** - Writable operation.\\n\\n* **Duplex** - Readable and writable operation.\\n\\n* **Transform** - Operates on written data and then reads out the result.\\n\\nAll Stream objects are instances of EventEmitter. Commonly used events are:\\n\\n* **data** - Triggered when there is data to read.\\n\\n* **end** - Triggered when there is no more data to read.\\n\\n* **error** - Triggered when an error occurs during receiving and writing.\\n\\n* **finish** - Triggered when all data has been written to the underlying system.\\n\\nThis tutorial will introduce commonly used stream operations.\\n\\n* * *\\n\\n## Reading Data from a Stream\\n\\nCommon readable streams include file read streams and network request response streams.\\n\\nCreate an input.txt file with the following content:\\n\\nOfficial website address: example.com\\nCreate a main.js file with the following code:\\n\\n## Instance\\n\\nvar fs = require("fs");\\n\\nvar data ='';\\n\\n// Create readable stream\\n\\nvar readerStream = fs.createReadStream('input.txt');\\n\\n// Set encoding to utf8.\\n\\n readerStream.setEncoding('UTF8');\\n\\n// Handle stream events --> data, end, and error\\n\\n readerStream.on('data',function(chunk){\\n\\ndata += chunk;\\n\\n});\\n\\nreaderStream.on('end',function(){\\n\\nconsole.log(data);\\n\\n});\\n\\nreaderStream.on('error',function(err){\\n\\nconsole.log(err.stack);\\n\\n});\\n\\nconsole.log("Program execution completed");\\n\\nThe execution result of the above code is as follows:\\n\\nProgram execution completed Official website address: example.com\\n\\n* * *\\n\\n## Writing to a Stream\\n\\nWritable streams are used to write data to a destination. Common writable streams include file write streams and network request sending streams.\\n\\nCreate a main.js file with the following code:\\n\\n## Instance\\n\\nvar fs = require("fs");\\n\\nvar data ='Official website address: example.com';\\n\\n// Create a writable stream, write to file output.txt in\\n\\nvar writerStream = fs.createWriteStream('output.txt');\\n\\n// Write data using utf8 encoding\\n\\n writerStream.write(data,'UTF8');\\n\\n// Mark the end of the file\\n\\n writerStream.end();\\n\\n// Handle stream events --> finishγerror\\n\\n writerStream.on('finish',function(){\\n\\n console.log("Writing completed.");\\n\\n});\\n\\nwriterStream.on('error',function(err){\\n\\nconsole.log(err.stack);\\n\\n});\\n\\nconsole.log("Program execution completed");\\n\\nThe above program will write the data of the data variable to the output.txt file. The code execution result is as follows:\\n\\n$ node main.js Program execution completedWriting completed.\\nView the contents of the output.txt file:\\n\\n$ cat output.txt Official website address: example.com\\n\\n* * *\\n\\n## Duplex Stream\\n\\nA duplex stream has both readable and writable capabilities.\\n\\nA typical duplex stream is a TCP socket.\\n\\n## Instance\\n\\nconst net = require('net');\\n\\n// Create a TCP server\\n\\nconst server = net.createServer((socket)=>{\\n\\n console.log('Client connected.');\\n\\n// Read client data\\n\\n socket.on('data',(data)=>{\\n\\n console.log('Received data:', data.toString());\\n\\n});\\n\\n// Send data to client\\n\\n socket.write('Hello, Client!n');\\n\\n// Listen to close event\\n\\n socket.on('end',()=>{\\n\\n console.log('Client disconnected.');\\n\\n});\\n\\n});\\n\\nserver.listen(3000,()=>{\\n\\n console.log('Server listening on port 3000.');\\n\\n});\\n\\n* * *\\n\\n## Transform Stream\\n\\nA transform stream is a special type of duplex stream that can modify or transform data. Common transform streams include compression and decompression streams.\\n\\n## Instance\\n\\nconst zlib = require('zlib');\\n\\nconst fs = require('fs');\\n\\n// Create a readable stream\\n\\nconst readableStream = fs.createReadStream('example.txt');\\n\\n// Create a transform stream (compression)\\n\\nconst gzip = zlib.createGzip();\\n\\n// Create a writable stream\\n\\nconst writableStream = fs.createWriteStream('example.txt.gz');\\n\\n// Pipe the readable stream to the transform stream, then to the writable stream\\n\\n readableStream.pipe(gzip).pipe(writableStream);\\n\\n// Listen to completion event\\n\\n writableStream.on('finish',()=>{\\n\\n console.log('File compressed successfully.');\\n\\n});\\n\\n* * *\\n\\n## Pipe Stream\\n\\nPiping provides a mechanism to connect an output stream to an input stream.\\n\\nIt is usually used to get data from one stream and pass the data to another stream.\\n\\n!(#)\\n\\nAs shown in the picture above, we compare the file to a bucket containing water, and the water is the content of the file. We use a pipe to connect two buckets so that water flows from one bucket into another, thus slowly realizing the process of copying a large file.\\n\\nIn the following example, we read the content of a file and write the content to another file.\\n\\nSet the content of the input.txt file as follows:\\n\\nOfficial website address: example.com Piping stream operation example\\nCreate a main.js file with the following code:\\n\\n## Instance\\n\\nvar fs = require("fs");\\n\\n// Create a readable stream\\n\\nvar readerStream = fs.createReadStream('input.txt');\\n\\n// Create a writable stream\\n\\nvar writerStream = fs.createWriteStream('output.txt');\\n\\n// Pipe read and write operations\\n\\n// Read input.txt File content, and write the content to output.txt In the file\\n\\n readerStream.pipe(writerStream);\\n\\nconsole.log("Program execution completed");\\n\\nThe code execution result is as follows:\\n\\n$ node main.js Program execution completed\\nView the contents of the output.txt file:\\n\\n$ cat output.txt Official website address: example.com Piping stream operation example\\n\\n* * *\\n\\n## Chain Stream\\n\\nChaining is a mechanism to connect an output stream to another stream and create a chain of multiple stream operations.\\n\\nChain streams are generally used for pipe operations.\\n\\nNext, we will use pipes and chaining to compress and decompress files.\\n\\nCreate a compress.js file with the following code:\\n\\n## Instance\\n\\nvar fs = require("fs");\\n\\nvar zlib = require('zlib');\\n\\n// Compress input.txt File as input.txt.gz\\n\\n fs.createReadStream('input.txt')\\n\\n .pipe(zlib.createGzip())\\n\\n .pipe(fs.createWriteStream('input.txt.gz'));\\n\\nconsole.log("File compression completed.");\\n\\nThe code execution result is as follows:\\n\\n$ node compress.js File compression completed.\\nAfter performing the above operations, we can see that the compressed file input.txt.gz of input.txt is generated in the current directory.\\n\\nNext, let's decompress the file, create a decompress.js file with the following code:\\n\\n## Instance\\n\\nvar fs = require("fs");\\n\\nvar zlib = require('zlib');\\n\\n// Decompress input.txt.gz File as input.txt\\n\\n fs.createReadStream('input.txt.gz')\\n\\n .pipe(zlib.createGunzip())\\n\\n .pipe(fs.createWriteStream('input.txt'));\\n\\nconsole.log("File decompression completed.");\\n\\nThe code execution result is as follows:\\n\\n$ node decompress.js File decompression completed.\\n\\n* * *\\n\\n## Pause and Resume\\n\\nReadable streams can pause and resume data reading.\\n\\n## Instance\\n\\nconst fs = require('fs');\\n\\nconst readableStream = fs.createReadStream('example.txt','utf8');\\n\\nreadableStream.on('data',(chunk)=>{\\n\\n console.log('Received chunk:', chunk);\\n\\n readableStream.pause();// Pause reading\\n\\n setTimeout(()=>{\\n\\n readableStream.resume();// Resume reading\\n\\n},1000);\\n\\n});\\n\\n* * *\\n\\n## Destroy\\n\\nStreams can be destroyed to release resources.\\n\\n## Instance\\n\\nconst fs = require('fs');\\n\\nconst readableStream = fs.createReadStream('example.txt','utf8');\\n\\nreadableStream.on('data',(chunk)=>{\\n\\n console.log('Received chunk:', chunk);\\n\\n readableStream.destroy();// Destroy stream\\n\\n});
YouTip