[node. js] Stream

Source: Internet
Author: User

Summary

Stream is an abstract interface, and there are many objects in node that implement this interface. For example, the request object that sends requests to an HTTP server is a stream.

Stream

Stream has four types of streams:

    1. Readable: readable operation.
    2. Writable: writable operation.
    3. Duplex: Can read and write operation.
    4. Transform: The operation is written to the data and then the result is read.

All stream objects are instances of eventemitter, commonly used events:

    1. Data: Triggered when there is a readable reading.
    2. End: Triggered when no more data is readable.
    3. Error: Triggered when a fault occurs during receive and write.
    4. Finish: Triggered when all data has been written to the underlying system.
Read

Create a Stream.txt file with the following content:

Hello Stream World ...

Create the Stream.js file with the following code:

//introduce the FS module. varFs=require ("FS");//Receive data VariablesvarData= ";//to create a readable streamvarReaderstream=fs.createreadstream ("Stream.txt");//set the encoding UTF8Readerstream.setencoding ("UTF8");//Handling Flow Event-->data,end,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 over");

Perform

Write

Create Write.js

//Introducing FSvarFs=require ("FS");varData= "Hello my stream world.";//creates a stream that can be written to. Write to File Output.txtvarWritestream=fs.createwritestream ("Output.txt");//writing files using UTF8Writestream.write (data, "UTF8"));//Mark file Endwritestream.end ();//Handling Flow EventsWritestream.on ("Finish",function() {Console.log ("Write Complete");}); Writestream.on ("Error",function(Err) {console.log (err.stack);}); Console.log ("Program execution complete");

Perform

At this point, you can see the content in the output.txt.

Pipe flow

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

An example

var fs=require ("FS"); // Create a readable stream var readerstream=fs.createreadstream ("Stream.txt"); // Create a writable stream var writestream=fs.createwritestream ("output.txt"); // Pipe Read and write operations // reads the contents of the Stream.txt file and writes the contents to the Output.txt file.  readerstream.pipe (writestream); Console.log ("Completion of program execution");

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.

An example

Compression and decompression of files with pipe and chain pull-I

Create Compress.js

var fs=require ("FS"); var zlib=require ("zlib"); // compress the Stream.txt file stream.txt.gzfs.createreadstream ("Stream.txt"). Pipe (Zlib.creategzip ()). PIPE ( Fs.createwritestream ("stream.txt.gz")); Console.log ("file compression complete");

Learning materials

Http://www.runoob.com/nodejs/nodejs-stream.html

[node. js] 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.