3

Stream

stream
data transmission means , there is an order
It is not like a traditional program that reads a file into memory at a time
But read the data block by block

such as:
1. Watch the video
Video resources are not returned all at once; instead, they flow from the server to the local player little by little
Play while streaming; this is the stream used

2. Read large files
If you use stream , create a read stream createReadStream through pipe link
can be read while returning to reduces the pressure on the server;
If you use readFile directly; the entire file will be returned at one time, and memory and network may be overwhelmed

species

writable stream : a stream that can write data. For example, fs.createWriteStream() can use the stream to write the file

readable stream : a stream of readable data. E.g. fs.createReadStream () from the file read content

duplex stream : can read and write stream. For example net.Socket

conversion stream : A stream that can modify or convert data when writing and reading data. For example, in a file compression operation, you can write compressed data to the file, and read decompressed data from the file

Scenes

Suitable for IO operation : http request & file operation

specific scenarios of have:

  1. The get request returns the file to the client
  2. File operations
  3. Low-level operations of some packaging tools

1.get request to return the file to the client

使用stream流返回文件,res也是一个stream对象,通过pipe管道将文件数据返回

const server = http.createServer(function (req, res) {
    const method = req.method; // 获取请求方法
    if (method === 'GET') { // get 请求
        const fileName = path.resolve(__dirname, 'data.txt');
        let stream = fs.createReadStream(fileName);
        stream.pipe(res); // 将 res 作为 stream 的 dest
    }
});
server.listen(8000);

2. File operations

创建一个可读数据流readStream,一个可写数据流writeStream,通过pipe管道把数据流转过去

const fs = require('fs')
const path = require('path')

// 两个文件名
const fileName1 = path.resolve(__dirname, 'data.txt')
const fileName2 = path.resolve(__dirname, 'data-bak.txt')
// 读取文件的 stream 对象
const readStream = fs.createReadStream(fileName1)
// 写入文件的 stream 对象
const writeStream = fs.createWriteStream(fileName2)
// 通过 pipe执行拷贝,数据流转
readStream.pipe(writeStream)
// 数据读取完成监听,即拷贝完成
readStream.on('end', function () {
    console.log('拷贝完成')
})

3. The underlying operation of some packaging tools

At present, some of the popular front-end packaging and construction tools are written through node.js. The process of packaging and building must be a process of frequent file operations, which is inseparable from streams, such as gulp.


NANA
94 声望7 粉丝

小魔女