Node.js-Stream (stream)

Stream is an abstract interface, and many objects in Node implement this interface. For example, the request object that initiates a request to the http server is a Stream, and stdout (standard output).

Node.js, Stream has four stream types:

  • Readable  - Readable operation.

  • Writable  - Writable operation.

  • Duplex  - Read and write operations.

  • Transform  - operations are written to data and then read out the result.

All Stream objects are instances of EventEmitter. Commonly used events are:

  • data  - fires when there is data to read.

  • end  - Fires when there is no more data to read.

  • error  - Fired when an error occurs during receiving and writing.

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

This tutorial will introduce you to common stream operations.

read data from the stream

Create an input.txt file with the following content:

csdn官网地址: www.csdn.net

Create a main.js file with the following code:

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 execution results are as follows:

程序执行完毕
csdn官网地址: www.csdn.net

write stream

Create a main.js file with the following code:

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

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

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

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

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

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

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

The above program will write the data of the data variable to the output.txt file. The code execution results are as follows:

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

View the contents of the output.txt file:

$ cat output.txt 
csdn官网地址: www.csdn.net

Create a main.js file with the following code:

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 execution results are as follows

$ node main.js 
程序执行完毕

View the contents of the output.txt file:

$ cat output.txt 
csdn官网地址: www.csdn.net
管道流操作实例

chain flow

Chaining is a mechanism for creating chains of multiple stream operations by connecting an output stream to another stream. Chained streams are generally used for pipeline operations.

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

Create a compress.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 execution results are as follows:

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

After performing the above operations, we can see that the compressed file input.txt.gz of input.txt is generated in the current directory.

Next, let's decompress the file and create a decompress.js file with the following code:

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 execution results are as follows:

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

Guess you like

Origin blog.csdn.net/jewels_w/article/details/130626935