Stream is an abstract interface, and many objects in node implement this interface, for example: the Request object that the HTTP server initiates requests is a stream, and stdout (standard output).
Node.js,stream has four flow types:
readable-readable operation writable-writable duplex-read-write operation transform-The operation is written to the data and the result is read.
All stream objects are instances of Eventemitter. The usual events are:
data-triggers when there is a data-readable trigger end-No more data is readable when the error-triggers when an error occurs during the receive and write process finish-all data has been written to the underlying system.
Reading data from the stream
varFS = require ("FS");vardata = ';//to create a readable streamvarReaderstream = Fs.createreadstream (' input.txt '));//set encoding to UTF8Readerstream.setencoding (' UTF8 ');//process Flow Event-->data, end, and errorReaderstream.on (' Data ',function(chunk) {data+=Chunk;}) Readerstream.on (' End ',function() {console.log (data);}) Readerstream.on (' Error ',function(Err) {console.log (err.stack);}) Console.log ("Program execution complete");
Write stream
Create the Main.js file with the following code:
varFS = require ("FS");vardata = ' Novice Tutorial ' website address: www.runoob.com ';//Create a stream that can be written to the file Output.txtvarWriterstream = Fs.createwritestream (' output.txt '));//writes data using UTF8 encoding. Writerstream.write (data, ' UTF8 '));//Mark file Endwriterstream.end ();//process Flow Event-->data, end, and errorWriterstream.on (' Finish ',function() {Console.log ("Write complete. "); }); Writersteam.on (' Error ',function(Err) {console.log (err.stack); }); Console.log ("Program execution complete");
Create the Main.js file with the following code: var fs = require ("FS"); // Create a readable stream var readerstream = Fs.createreadstream (' intput.txt '); // Create a writable stream var writerstream = Fs.createrwritestream (' output.txt '); // Pipe Read and write operations // reads the Input.txt content and writes the content to the Output.txt file. readersteam.pipe (writerstream); Console.log ("Completion of program execution");
Chained flow
Chaining is a mechanism for connecting output flows to another stream and creating multiple action chains, which are typically used for pipeline operations.
Next we are using pipelines and chains to compress and decompress files.
Create the Compress.js file with the following code:
var fs = require ("FS"); var zlib = require ("zlib"); // The compressed Input.txt file is input.txt.gzfs.createreadstream (' input.txt ') . Pipe (ZLIB.CREATEGIZP ()) . Pipe (Fs.createwritestream (' input.txt.gz ')); Console.log ("file compression complete. ");
var fs = require ("FS"); var zlib = require (' zlib '); // Unzip the input.txt.gz file to Inpu.txtfs.createreadstream (' input.txt.gz ') . Pipe (Zlib.creategunzip ()) . Pipe (Fs.createwritestream (' input.txt ')); Console.log ("file decompression complete");
node. js Stream