Node .js Stream

Stream is a .js module in Node's network and is widely used.

Stream is an abstract interface that is implemented by many objects in Node. F or example, the request object that initiates a request to the http server is a Stream, and stdout (standard output).

This abstract interface is readable, writeable, or both readable and writeable, through which we can interact with disk files, sockets, HTTP requests, and implement the ability of data to flow from one place to another.

Node .js, Stream has four streaming types:

  • Readable - Readable action.

  • Writable - Writeable.

  • Duplex - Readable and writeable.

  • Transform - The operation is written to the data and the results are read out.

All Stream objects are instances of EventEmitter. Common events are:

  • data - triggered when data is readable.

  • end - triggered when no more data is readable.

  • Error - Triggered when an error occurs during reception and writing.

  • Finish - Triggered when all data has been written to the underlying system.

This tutorial will introduce you to common flow operations.


Read data from the stream

Create an input .txt file that looks like this:

W3Cschool教程官网地址:www.w3cschool.cn

Create a main .js file, code as follows:

var fs = require("fs");
var data = '';

// 创建可读流
var readerStream = fs.createReadStream('input.txt');

// 设置编码为 utf8。
readerStream.setEncoding('UTF8');

// 处理流事件 --> data, end, and error
readerStream.on('data', function(chunk) {
   data += chunk;
});

readerStream.on('end',function(){
   console.log(data);
});

readerStream.on('error', function(err){
   console.log(err.stack);
});

console.log("程序执行完毕");

The above code executes as follows:

程序执行完毕
W3Cschool教程官网地址:www.w3cschool.cn

Write to the stream

Create a main .js file, code as follows:

var fs = require("fs");
var data = 'W3Cschool教程官网地址:www.w3cschool.cn';

// 创建一个可以写入的流,写入到文件 output.txt 中
var writerStream = fs.createWriteStream('output.txt');

// 使用 utf8 编码写入数据
writerStream.write(data,'UTF8');

// 标记文件末尾
writerStream.end();

// 处理流事件 --> data, end, and error
writerStream.on('finish', function() {
    console.log("写入完成。");
});

writerStream.on('error', function(err){
   console.log(err.stack);
});

console.log("程序执行完毕");

The above program writes data from the data variable to the output .txt file. The code executes as follows:

$ node main.js 
程序执行完毕
写入完成。

View the contents .txt the output file:

$ cat output.txt 
W3Cschool教程官网地址:www.w3cschool.cn

The pipeline flow

The pipeline provides a mechanism for output flow to input flow. T ypically, we use to get data from one stream and pass it to another.

Node .js Stream

As shown in the picture above, we liken the file to a bucket of water, and water is the content of the file, we use a pipe to connect two buckets so that water from one bucket to another, so that the process of copying large files is slowly realized.

The following example lets us read the contents of one file and write the contents to another file.

Set up input.txt file is as follows:

W3Cschool教程官网地址:www.w3cschool.cn
管道流操作实例

Create a main .js file, code as follows:

var fs = require("fs");

// 创建一个可读流
var readerStream = fs.createReadStream('input.txt');

// 创建一个可写流
var writerStream = fs.createWriteStream('output.txt');

// 管道读写操作
// 读取 input.txt 文件内容,并将内容写入到 output.txt 文件中
readerStream.pipe(writerStream);

console.log("程序执行完毕");

The code executes as follows:

$ node main.js 
程序执行完毕

View the contents .txt the output file:

$ cat output.txt 
W3Cschool教程官网地址:www.w3cschool.cn
管道流操作实例

Chain flow

A chain is a mechanism by connecting output streams to another stream and creating multiple pairs of flow chains of operation. Chain streams are typically used for pipe operations.

Next we use pipes and chains to compress and unzip files.

Create a .js file with the following code:

var fs = require("fs");
var zlib = require('zlib');

// 压缩 input.txt 文件为 input.txt.gz
fs.createReadStream('input.txt')
  .pipe(zlib.createGzip())
  .pipe(fs.createWriteStream('input.txt.gz'));
  
console.log("文件压缩完成。");

The code executes as follows:

$ node compress.js 
文件压缩完成。

After doing this, we can see that the input file input with input .txt is generated in the .txt.gz.

Next, let's unzipp the file and create a decompress .js file, as follows:

var fs = require("fs");
var zlib = require('zlib');

// 解压 input.txt.gz 文件为 input.txt
fs.createReadStream('input.txt.gz')
  .pipe(zlib.createGunzip())
  .pipe(fs.createWriteStream('input.txt'));
  
console.log("文件解压完成。");

The code executes as follows:

$ node decompress.js 
文件解压完成。