Node.js 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, as well as stdout (standard output).
Node.js, Stream has four stream types:
Readable - Readable operation.
Writable - Writable operations.
Duplex - Operations can be read and written.
Transform - Operations are written data and then read the results.
All Stream objects are instances of EventEmitter. Commonly used events are:
data - triggered when data is readable.
#end - Fires when there is no more data to read.
error - Fires when an error occurs during reception and writing.
finish - Fires when all data has been written to the underlying system.
This tutorial will introduce you to commonly used stream operations.
Read data from the stream
Create the input.txt file with the following content:
php中文网官网地址:www.php.cn
Create the 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 execution result of the above code is as follows:
程序执行完毕 php中文网官网地址:www.php.cn
Writing stream
Create the main.js file, the code is as follows:
var fs = require("fs"); var data = 'php中文网官网地址:www.php.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 will use the data variable The data is written to the output.txt file. The code execution result is as follows:
$ node main.js 程序执行完毕 写入完成。
View the contents of the output.txt file:
$ cat output.txt php中文网官网地址:www.php.cn
Pipeline flow
Pipeline provides a mechanism for output flow to input stream. Usually we use it to get data from one stream and pass the data to another stream.As shown in the picture above, we compare the file to a bucket filled with water, and the water is the content in the file. We use a pipe to connect it The two buckets allow water to flow from one bucket to the other, thus slowly realizing the copying process of large files.
In the following example, we read the contents of a file and write the contents to another file.
Set the input.txt file content as follows:
php中文网官网地址:www.php.cn 管道流操作实例
Create the 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 Contents of the output.txt file:
$ cat output.txt php中文网官网地址:www.php.cn 管道流操作实例
Chained stream
Chaining is a mechanism that connects an output stream to another stream and creates multiple chains of operations on each stream. Chained flows are generally used for pipeline operations.
Next we will use pipes and chains to compress and decompress files.
Create the compress.js file, the code is 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("文件压缩完成。");
The code execution result is as follows:
$ node compress.js 文件压缩完成。
After executing the above operation, we can see that the input is generated in the current directory The .txt compressed file input.txt.gz.
Next, let us decompress the file and create the decompress.js file. The code is 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 execution result is as follows:
$ node decompress.js 文件解压完成。