Latest web development tutorials

Node.js Stream (stream)

Stream is an abstract interface, Node has many objects implement this interface. For example, the server that initiated the request for http request object is a Stream, as well as stdout (standard output).

Node.js, Stream There are four types of flow:

  • Readable - Readable operation.

  • Writable - write operations.

  • Duplex - read and write operations.

  • Transform - operating data to be written and then read out the results.

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

  • data - triggered when data read time.

  • end - without triggering more readable data.

  • error - triggered when an error occurs in the reception and the writing process.

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

This tutorial will introduce common stream operations.


Read data from the stream

Create input.txt file, as follows:

本教程官网地址:www.w3big.com

Create main.js file, 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 is executed as follows:

程序执行完毕
本教程官网地址:www.w3big.com

Write stream

Create main.js file, as follows:

var fs = require("fs");
var data = '本教程官网地址:www.w3big.com';

// 创建一个可以写入的流,写入到文件 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 data will be written to the variable data file output.txt. Code execution results are as follows:

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

Output.txt view the contents of the file:

$ cat output.txt 
本教程官网地址:www.w3big.com

Pipeline flow

Pipeline provides a mechanism for the output flow input stream. We usually used to obtain data and transfer the data to another stream from a stream.

As shown in the picture above, we compared the documents bucket filled with water, and the water is the contents of the documents, so that we connect two buckets of water from a bucket into the other bucket with a pipe (pipe), so slowly realized the replication process large files.

The following examples we want to write to another file by reading the contents of a file and the contents.

Setting input.txt document reads as follows:

本教程官网地址:www.w3big.com
管道流操作实例

Create main.js file, 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("程序执行完毕");

Code execution results are as follows:

$ node main.js 
程序执行完毕

Output.txt view the contents of the file:

$ cat output.txt 
本教程官网地址:www.w3big.com
管道流操作实例

Chain Flow

Chain is another flowing stream and create multiple streams operating mechanism chain by connecting the output. Chain stream generally used for pipeline operation.

Next we are pipe and chain to compress and decompress files.

Create compress.js file, as follows:

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("文件压缩完成。");

Code execution results are as follows:

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

After executing the above operation, we can see the current directory created under input.txt compressed file input.txt.gz.

Next, let's unpack the files, create 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("文件解压完成。");

Code execution results are as follows:

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