PHP前端开发

流:Nodejs

百变鹏仔 4天前 #JavaScript
文章标签 Nodejs

node.js 中的流完整指南

node.js 中的流是一种以连续方式处理数据读写的强大方法。它们使您能够有效地处理数据,特别是在处理大量信息或 i/o 操作时。本指南将介绍流的类型、如何使用它们以及实际示例,以帮助您了解流在 node.js 中的工作原理。

什么是流?

流是允许您以连续方式从源读取数据或将数据写入目标的对象。它们非常适合逐个处理数据,而不是一次读取或写入整个文件或缓冲区。这在处理大型数据集时特别有用,因为它可以显着减少内存使用量。

关键概念

流的类型

  1. 可读流:这些流允许您读取数据。示例包括 fs.createreadstream() 和 http.incomingmessage.

  2. 可写流:这些流允许您写入数据。示例包括 fs.createwritestream() 和 http.serverresponse.

  3. 双工流:这些流可以读取和写入数据。示例包括 tcp 套接字和 net.duplex。

  4. 转换流:这是一种双工流,可以在读取或写入数据时修改数据。示例包括用于压缩的 zlib.creategzip()。

创建可读流

您可以使用内置的 fs 模块创建可读流来读取文件,或者使用stream.readable 创建自定义可读流。

示例:使用可读流读取文件

const fs = require('fs');// create a readable streamconst readablestream = fs.createreadstream('example.txt', { encoding: 'utf8' });// handling the 'data' eventreadablestream.on('data', (chunk) => {    console.log('new chunk received:', chunk);});// handling the 'end' eventreadablestream.on('end', () => {    console.log('no more data to read.');});

示例:自定义可读流

const { readable } = require('stream');class myreadablestream extends readable {    constructor(options) {        super(options);        this.current = 0;    }    _read(size) {        if (this.current  {    console.log('received:', chunk.tostring());});

创建可写流

您可以使用 fs 模块或扩展stream.writable 类来创建可写流。

示例:使用可写流写入文件

const fs = require('fs');// create a writable streamconst writablestream = fs.createwritestream('output.txt');// write data to the streamwritablestream.write('hello, world!\n');writablestream.write('writing to a file using streams.\n');// end the streamwritablestream.end(() => {    console.log('finished writing to file.');});

示例:自定义可写流

const { writable } = require('stream');class mywritablestream extends writable {    _write(chunk, encoding, callback) {        console.log('writing:', chunk.tostring());        callback(); // call when done    }}const mywritablestream = new mywritablestream();mywritablestream.write('hello, world!\n');mywritablestream.write('writing to custom writable stream.\n');mywritablestream.end();

使用双工流

双工流可以同时读取和写入数据。一个常见的用例是 tcp 套接字。

示例:创建双工流

const { duplex } = require('stream');class myduplexstream extends duplex {    _read(size) {        this.push('data from duplex stream\n');        this.push(null); // no more data    }    _write(chunk, encoding, callback) {        console.log('received:', chunk.tostring());        callback();    }}const myduplexstream = new myduplexstream();myduplexstream.on('data', (chunk) => {    console.log('reading:', chunk.tostring());});// write to the duplex streammyduplexstream.write('hello, duplex!\n');myduplexstream.end();

使用转换流

转换流对于修改流经流的数据非常有用。例如,您可以使用转换流来压缩数据。

示例:创建转换流

const { transform } = require('stream');class mytransformstream extends transform {    _transform(chunk, encoding, callback) {        const upperchunk = chunk.tostring().touppercase();        this.push(upperchunk);        callback();    }}const mytransformstream = new mytransformstream();mytransformstream.on('data', (chunk) => {    console.log('transformed:', chunk.tostring());});// pipe data through the transform streamprocess.stdin.pipe(mytransformstream).pipe(process.stdout);

管道流

流的强大功能之一是将它们通过管道连接在一起的能力。管道允许您将可读流连接到可写流,这使得传输数据变得容易。

示例:管道流

const fs = require('fs');// create a readable streamconst readablestream = fs.createreadstream('input.txt');// create a writable streamconst writablestream = fs.createwritestream('output.txt');// pipe the readable stream to the writable streamreadablestream.pipe(writablestream);writablestream.on('finish', () => {    console.log('data has been written to output.txt');});

node.js 中的流事件

1. 可读流事件

可读流会发出几个重要事件来帮助您管理数据流:

示例:可读流事件

const fs = require('fs');const readablestream = fs.createreadstream('example.txt');readablestream.on('data', (chunk) => {    console.log('received chunk:', chunk.tostring());});readablestream.on('end', () => {    console.log('no more data to read.');});readablestream.on('error', (err) => {    console.error('error occurred:', err);});readablestream.on('close', () => {    console.log('stream closed.');});

2. 可写流事件

可写流也会发出几个事件:

示例:可写流事件

const fs = require('fs');const writablestream = fs.createwritestream('output.txt');writablestream.on('finish', () => {    console.log('all data has been written to output.txt');});writablestream.on('error', (err) => {    console.error('error occurred:', err);});// writing datawritablestream.write('hello, world!\n');writablestream.write('writing to a file using streams.\n');writablestream.end(); // call end to finish the writing process

3. 转换流事件

转换流从可读流和可写流继承事件,并且它们发出:

示例:转换流事件

const { Transform } = require('stream');class MyTransformStream extends Transform {    _transform(chunk, encoding, callback) {        const upperChunk = chunk.toString().toUpperCase();        this.push(upperChunk);        callback();    }}const myTransformStream = new MyTransformStream();myTransformStream.on('data', (chunk) => {    console.log('Transformed chunk:', chunk.toString());});myTransformStream.on('end', () => {    console.log('No more data to transform.');});myTransformStream.on('error', (err) => {    console.error('Error occurred:', err);});// Write data to the transform streammyTransformStream.write('Hello, World!\n');myTransformStream.write('Transforming this text.\n');myTransformStream.end(); // End the stream

活动概要

结论

node.js 中的流提供了一种强大且高效的方式来连续处理数据。它们允许您逐段读取和写入数据,这使得它们对于 i/o 操作和处理大型数据集特别有用。了解如何创建和使用不同类型的流,以及如何处理事件,将帮助您在 node.js 中构建更高效、可扩展的应用程序。

无论您是创建可读、可写、双工还是转换流,流 api 的灵活性都允许您以最适合应用程序需求的方式处理数据。