Node.js, being asynchronous and event-driven, excels at I/O-bound operations. Leveraging Node.js streams significantly simplifies these tasks by efficiently processing data in smaller chunks. Let's delve into the world of streams and see how they streamline I/O.
Key Concepts:
- Node.js streams, asynchronous and event-driven, optimize I/O by handling data in manageable portions.
- Streams are classified as Readable, Writable, or Duplex (both readable and writable). Readable streams fetch data from a source; writable streams send data to a destination.
- The
pipe()
function is invaluable, facilitating seamless data transfer between source and destination without manual flow management. - Methods like
Readable.pause()
,Readable.resume()
, andreadable.unpipe()
offer granular control over data flow, enhancing stream functionality.
Understanding Streams:
Streams are analogous to Unix pipes, enabling effortless data transfer from source to destination. Essentially, a stream is an EventEmitter
with specialized methods. The implemented methods determine whether a stream is Readable, Writable, or Duplex. Readable streams provide data input; writable streams handle data output.
You've likely encountered streams in Node.js already. In an HTTP server, the request is a readable stream, and the response is a writable stream. The fs
module provides both readable and writable file stream capabilities.
This article focuses on readable and writable streams; duplex streams are beyond its scope.
Readable Streams:
A readable stream reads data from a source (a file, in-memory buffer, or another stream). Being EventEmitter
s, they trigger various events. We utilize these events to interact with the streams.
Reading from Streams:
The most common approach is to listen for the data
event and attach a callback. When data is available, the data
event fires, executing the callback.
const fs = require('fs'); const readableStream = fs.createReadStream('file.txt'); let data = ''; readableStream.on('data', (chunk) => { data += chunk; }); readableStream.on('end', () => { console.log(data); });
fs.createReadStream()
creates a readable stream. Initially static, it begins flowing upon attaching a data
event listener. Data chunks are then passed to the callback. The frequency of data
events is determined by the stream implementation (e.g., an HTTP request might emit an event per few KB, while a file stream might emit per line).
The end
event signals the end of data.
Alternatively, repeatedly call read()
on the stream instance until all data is read:
const fs = require('fs'); const readableStream = fs.createReadStream('file.txt'); let data = ''; readableStream.on('data', (chunk) => { data += chunk; }); readableStream.on('end', () => { console.log(data); });
read()
retrieves data from the internal buffer. It returns null
when no data remains. The readable
event indicates data availability.
Setting Encoding:
Data is typically a Buffer
object. For strings, use Readable.setEncoding()
:
const fs = require('fs'); const readableStream = fs.createReadStream('file.txt'); let data = ''; let chunk; readableStream.on('readable', () => { while ((chunk = readableStream.read()) !== null) { data += chunk; } }); readableStream.on('end', () => { console.log(data); });
This interprets data as UTF-8, passing it as a string to the callback.
Piping:
Piping simplifies data transfer between source and destination:
const fs = require('fs'); const readableStream = fs.createReadStream('file.txt'); let data = ''; readableStream.setEncoding('utf8'); readableStream.on('data', (chunk) => { data += chunk; }); readableStream.on('end', () => { console.log(data); });
pipe()
handles data flow automatically.
Chaining:
Streams can be chained:
const fs = require('fs'); const readableStream = fs.createReadStream('file1.txt'); const writableStream = fs.createWriteStream('file2.txt'); readableStream.pipe(writableStream);
This decompresses input.txt.gz
and writes the result to output.txt
.
Additional Readable Stream Methods:
Readable.pause()
: Pauses the stream.Readable.resume()
: Resumes a paused stream.readable.unpipe()
: Removes destination streams from the pipe.
Writable Streams:
Writable streams send data to a destination. Like readable streams, they are EventEmitter
s.
Writing to Streams:
Use write()
to send data:
const fs = require('fs'); const zlib = require('zlib'); fs.createReadStream('input.txt.gz') .pipe(zlib.createGunzip()) .pipe(fs.createWriteStream('output.txt'));
write()
returns a boolean indicating success. If false, the stream is temporarily full; wait for the drain
event before writing more.
End of Data:
Call end()
to signal the end of data. The finish
event is emitted after all data is flushed. You cannot write after calling end()
.
Important Writable Stream Events:
-
error
: Indicates an error. -
pipe
: Emitted when a readable stream is piped. -
unpipe
: Emitted whenunpipe()
is called on the readable stream.
Conclusion:
Streams are a powerful feature in Node.js, enhancing I/O efficiency. Understanding streams, piping, and chaining enables writing clean, performant code.
Node.js Streams FAQ:
-
What are Node.js streams? They are objects that allow for efficient, incremental processing of data, avoiding loading entire datasets into memory.
-
Main types of Node.js streams? Readable, Writable, Duplex, and Transform.
-
Creating a Readable stream? Use
stream.Readable
and implement the_read
method. -
Common use cases for Readable streams? Reading large files, processing data from HTTP requests, real-time data handling.
-
Creating a Writable stream? Use
stream.Writable
and implement the_write
method. -
Common uses of Writable streams? Saving data to files, sending data to services.
-
Duplex stream? Combines Readable and Writable functionality.
-
Transform streams? Modify data as it passes through (e.g., compression, encryption).
-
Piping data between streams? Use the
.pipe()
method. -
Best practices for working with Node.js streams? Use them for large datasets, handle errors and backpressure, and consider
util.promisify
for promise-based operations.
The above is the detailed content of The Basics of Node.js Streams. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Java and JavaScript are different programming languages, each suitable for different application scenarios. Java is used for large enterprise and mobile application development, while JavaScript is mainly used for web page development.

JavaScriptcommentsareessentialformaintaining,reading,andguidingcodeexecution.1)Single-linecommentsareusedforquickexplanations.2)Multi-linecommentsexplaincomplexlogicorprovidedetaileddocumentation.3)Inlinecommentsclarifyspecificpartsofcode.Bestpractic

The following points should be noted when processing dates and time in JavaScript: 1. There are many ways to create Date objects. It is recommended to use ISO format strings to ensure compatibility; 2. Get and set time information can be obtained and set methods, and note that the month starts from 0; 3. Manually formatting dates requires strings, and third-party libraries can also be used; 4. It is recommended to use libraries that support time zones, such as Luxon. Mastering these key points can effectively avoid common mistakes.

PlacingtagsatthebottomofablogpostorwebpageservespracticalpurposesforSEO,userexperience,anddesign.1.IthelpswithSEObyallowingsearchenginestoaccesskeyword-relevanttagswithoutclutteringthemaincontent.2.Itimprovesuserexperiencebykeepingthefocusonthearticl

JavaScriptispreferredforwebdevelopment,whileJavaisbetterforlarge-scalebackendsystemsandAndroidapps.1)JavaScriptexcelsincreatinginteractivewebexperienceswithitsdynamicnatureandDOMmanipulation.2)Javaoffersstrongtypingandobject-orientedfeatures,idealfor

JavaScripthassevenfundamentaldatatypes:number,string,boolean,undefined,null,object,andsymbol.1)Numbersuseadouble-precisionformat,usefulforwidevaluerangesbutbecautiouswithfloating-pointarithmetic.2)Stringsareimmutable,useefficientconcatenationmethodsf

Event capture and bubble are two stages of event propagation in DOM. Capture is from the top layer to the target element, and bubble is from the target element to the top layer. 1. Event capture is implemented by setting the useCapture parameter of addEventListener to true; 2. Event bubble is the default behavior, useCapture is set to false or omitted; 3. Event propagation can be used to prevent event propagation; 4. Event bubbling supports event delegation to improve dynamic content processing efficiency; 5. Capture can be used to intercept events in advance, such as logging or error processing. Understanding these two phases helps to accurately control the timing and how JavaScript responds to user operations.

Java and JavaScript are different programming languages. 1.Java is a statically typed and compiled language, suitable for enterprise applications and large systems. 2. JavaScript is a dynamic type and interpreted language, mainly used for web interaction and front-end development.
