node. JS Stream (Stream)

Source: Internet
Author: User
Tags gz file readable

Stream is an abstract interface, and there are many objects in Node that implement this interface. For example, the Request object requesting the HTTP server is a Stream and stdout (standard output).

Node.js,stream has four flow types:

    • readable -readable operation.

    • writable -writable operation.

    • Duplex -readable writable operation.

    • Transform -The operation is written to the data and then the result is read.

All Stream objects are instances of Eventemitter. The usual events are:

    • data-triggered when there is a read.

    • End -triggers when there is no more data to read.

    • Error-triggered when a fault occurs during receive and write.

    • finish -triggers when all data has been written to the underlying system.

This tutorial will introduce you to common flow operations.

Reading data from the stream

Create a Input.txt file with the following content:

Stream is an abstract interface.

Create the Main.js file with the following code:

VarFs= Require("FS");VarData= ‘‘;To create a readable streamVarReaderstream=Fs.Createreadstream(' Input.txt ');Set the encoding to UTF8.Readerstream.Setencoding(' UTF8 ');Process Stream Events--data, end, and errorReaderstream.On(' Data ', function(Chunk) {Data+=Chunk;});Readerstream.On(' End ',function(){ Console.datareaderstream. ( ' ERROR ' , function< Span class= "pun" > (err Console.log (err. Stackconsole. "program execution complete"         

The result of the above code execution is as follows:

The program finishes executing Stream is an abstract interface.
Write stream

Create the Main.js file with the following code:

VarFs= Require("FS");VarData= ' Stream is an abstract interface ';Create a stream that can be written to the file output.txtVarWriterstream=Fs.Createwritestream(' Output.txt ');Writing data using UTF8 encodingWriterstream.Write(Data,' UTF8 ');Mark File EndWriterstream.End();Process Stream Events--data, end, and errorWriterstream.On(' Finish ', function() { Console. ( "write complete. "writerstream. ( ' ERROR ' , function< Span class= "pun" > (err Console.log (err. Stackconsole. "program execution complete"         

The above program will write the data variables to the Output.txt file. The code execution results are as follows:

$ node main.  The program finishes writing. 

View the contents of the Output.txt file:

$ cat Output.  TXT Stream is an abstract interface
Pipe flow

The pipeline provides a mechanism for outputting a stream to the input stream. Typically we use it to fetch data from one stream and pass it to another stream.

As shown in the picture above, we compare the file to a water-filled bucket, and the water is the contents of the file, we use a pipe (pipe) to connect two barrels to the water from one bucket into another, so that slowly realize the large file copy process.

The following example we read a file content and write the content to another file.

Set the contents of the Input.txt file as follows:

Stream is an abstract interface pipeline flow operation Instance

Create the Main.js file with the following code:

VarFs= Require("FS");Create a readable streamVarReaderstream=Fs.createreadstream ( ' input.txt ' //create a writable stream var Writerstream = Fs. ' output.txt ' //pipeline read and write operations //read input.txt file contents and write content to output.txt file readerstream. Pipewriterstream. Log "program execution complete"          

The code execution results are as follows:

$ node main.  Program execution Complete 

View the contents of the Output.txt file:

$ cat Output.  Stream is an abstract interface pipeline flow operation instance  
Chained flow

A chain is a mechanism that flows through the connection output to another stream and creates multiple pairs of flow operation chains. Chained streams 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:

VarFs= Require("FS");VarZlib= Require(' Zlib '//compressed input.txt file for Input.txt.gzfs.createreadstream ( ' input.txt '  . (zlib. Creategzip ()  . (fs. Createwritestream ' input.txt.gz ' . Log ( "file compression complete. "               

The code execution results are as follows:

$ node Compress.  File compression is complete. 

After performing the above operation, we can see the Input.txt compressed file input.txt.gz generated in the current directory.

Next, let's unzip the file and create the Decompress.js file with the following code:

VarFs= Require("FS");VarZlib= Require(' Zlib '//unzip input.txt.gz file for Input.txtfs.createreadstream ( ' input.txt.gz '  . (zlib. Creategunzip ()  . (fs. Createwritestream ' input.txt ' . Log (" file is completed. "               

The code execution results are as follows:

$ node Decompress.  File decompression completed. 

node. JS Stream (Stream)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.