node. js Stream

Source: Internet
Author: User
Tags gz file

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

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.