Node.js Stream (stream)
Node.js's Stream is an abstract interface for handling streaming data, widely used in scenarios such as file operations and network communication.
A major advantage of stream-based data processing is that data can be processed while it is being transferred, without waiting for the entire data set to finish loading. This allows Node.js to handle large amounts of data efficiently without using too much memory.

Stream is an abstract interface, and many objects in Node implement it. For example, the request object of an HTTP server request is a Stream, as is stdout (standard output).
Node.js's Stream (stream) is a way of processing data that lets you handle it as a stream rather than loading it all into memory at once. This is very useful for handling large amounts of data or achieving efficient data transfer.
In Node.js, there are four stream types:
-
Readable - Readable operations.
-
Writable - Writable operations.
-
Duplex - Readable and writable operations.
-
Transform - Data is written in, and then the result is read out.
All Stream objects are instances of EventEmitter. Common events include:
-
data - Triggered when data is available to read.
-
end - Triggered when there is no more data to read.
-
error - Triggered when an error occurs during receiving or writing.
-
finish - Triggered when all data has been written to the underlying system.
This tutorial introduces common stream operations.
Reading data from a stream
Common readable streams include file read streams and network request response streams.
Create an input.txt file with the following content:
Runoob Tutorial官网地址:www.runoob.com
Create a file named main.js with the following code:
Example
var
fs
=
require
(
"fs"
)
;
var
data
=
''
;
// Create a readable stream
var
readerStream
=
fs.
createReadStream
(
'input.txt'
)
;
// Set the encoding to utf8.
readerStream.
setEncoding
(
'UTF8'
)
;
// Handle stream events --> 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
(
"Program execution complete"
)
;
The result of executing the above code is as follows:
程序执行完毕
Runoob Tutorial官网地址:www.runoob.com
Write streams
Writable streams are used to write data to a destination; common writable streams include file write streams and network request send streams.
Create a file named main.js with the following code:
Example
var
fs
=
require
(
"fs"
)
;
var
data
=
'Runoob Tutorial official website: www.runoob.com'
;
// Create a stream that can be written to, writing to the file output.txt
var
writerStream
=
fs.
createWriteStream
(
'output.txt'
)
;
// Write data using utf8 encoding
writerStream.
write
(
data
,
'UTF8'
)
;
// Mark the end of the file
writerStream.
end
(
)
;
// Handle stream events --> finish, error
writerStream.
on
(
'finish'
,
function
(
)
{
console.
log
(
"Write complete."
)
;
}
)
;
writerStream.
on
(
'error'
,
function
(
err
)
{
console.
log
(
err.
stack
)
;
}
)
;
console.
log
(
"Program execution complete"
)
;
The above program writes the data from the data variable to the output.txt file. The execution result is as follows:
$ node main.js
程序执行完毕
写入完成。
View the contents of the output.txt file:
$ cat output.txt
Runoob Tutorial官网地址:www.runoob.com
Duplex streams
A duplex stream has both readable and writable capabilities.
A typical duplex stream is a TCP socket.
Example
const
net
=
require
(
'net'
)
;
// Create a TCP server
const
server
=
net.
createServer
(
(
socket
)
=>
{
console.
log
(
'Client connected.'
)
;
// Read client data
socket.
on
(
'data'
,
(
data
)
=>
{
console.
log
(
'Received data:'
,
data.
toString
(
)
)
;
}
)
;
// Send data to the client
socket.
write
(
'Hello, Client!\n'
)
;
// Listen for the close event
socket.
on
(
'end'
,
(
)
=>
{
console.
log
(
'Client disconnected.'
)
;
}
)
;
}
)
;
server.
listen
(
3000
,
(
)
=>
{
console.
log
(
'Server listening on port 3000.'
)
;
}
)
;
Transform streams
A transform stream is a special kind of duplex stream that can modify or transform data. Common transform streams include compression and decompression streams.
Example
const
zlib
=
require
(
'zlib'
)
;
const
fs
=
require
(
'fs'
)
;
// Create a readable stream
const
readableStream
=
fs.
createReadStream
(
'example.txt'
)
;
// Create a transform stream (compression)
const
gzip
=
zlib.
createGzip
(
)
;
// Create a writable stream
const
writableStream
=
fs.
createWriteStream
(
'example.txt.gz'
)
;
// Pipe the readable stream to the transform stream, then to the writable stream
readableStream.
pipe
(
gzip
)
.
pipe
(
writableStream
)
;
// Listen for the finish event
writableStream.
on
(
'finish'
,
(
)
=>
{
console.
log
(
'File compressed successfully.'
)
;
}
)
;
Piped streams
A pipe provides a mechanism for connecting an output stream to an input stream.
We usually use it to get data from one stream and pass it to another.
As shown in the image above, we compare a file to a bucket of water, and the water is the content of the file. We use a pipe to connect the two buckets so that water flows from one bucket to the other, gradually achieving the copying of a large file.
In the following example, we read the content of one file and write it to another file.
Set the content of the input.txt file as follows:
Runoob Tutorial官网地址:www.runoob.com
管道流操作实例
Create a file named main.js with the following code:
Example
var
fs
=
require
(
"fs"
)
;
// Create a readable stream
var
readerStream
=
fs.
createReadStream
(
'input.txt'
)
;
// Create a writable stream
var
writerStream
=
fs.
createWriteStream
(
'output.txt'
)
;
// Pipe read/write operations
// Read the content of input.txt and write it to output.txt
readerStream.
pipe
(
writerStream
)
;
console.
log
(
"Program execution complete"
)
;
The execution result is as follows:
$ node main.js
程序执行完毕
View the contents of the output.txt file:
$ cat output.txt
Runoob Tutorial官网地址:www.runoob.com
管道流操作实例
Chained streams
Chaining is a mechanism that connects an output stream to another stream and creates a chain of multiple stream operations.
Chained streams are generally used for pipe operations.
Next, we will use pipes and chaining to compress and decompress files.
Create a compress.js file with the following code:
Example
var
fs
=
require
(
"fs"
)
;
var
zlib
=
require
(
'zlib'
)
;
// Compress the input.txt file into input.txt.gz
fs.
createReadStream
(
'input.txt'
)
.
pipe
(
zlib.
createGzip
(
)
)
.
pipe
(
fs.
createWriteStream
(
'input.txt.gz'
)
)
;
console.
log
(
"File compression complete."
)
;
The execution result is as follows:
$ node compress.js
文件压缩完成。
After running the above operations, we can see that a compressed file of input.txt, input.txt.gz, has been generated in the current directory.
Next, let's decompress the file. Create a decompress.js file with the following code:
Example
var
fs
=
require
(
"fs"
)
;
var
zlib
=
require
(
'zlib'
)
;
// Decompress the input.txt.gz file into input.txt
fs.
createReadStream
(
'input.txt.gz'
)
.
pipe
(
zlib.
createGunzip
(
)
)
.
pipe
(
fs.
createWriteStream
(
'input.txt'
)
)
;
console.
log
(
"File decompression complete."
)
;
The execution result is as follows:
$ node decompress.js
文件解压完成。
Pause and resume
A readable stream can pause and resume reading data.
Example
const
fs
=
require
(
'fs'
)
;
const
readableStream
=
fs.
createReadStream
(
'example.txt'
,
'utf8'
)
;
readableStream.
on
(
'data'
,
(
chunk
)
=>
{
console.
log
(
'Received chunk:'
,
chunk
)
;
readableStream.
pause
(
)
;
// Pause reading
setTimeout
(
(
)
=>
{
readableStream.
resume
(
)
;
// Resume reading
}
,
1000
)
;
}
)
;
Destroy
A stream can be destroyed to release resources.
Example
const
fs
=
require
(
'fs'
)
;
const
readableStream
=
fs.
createReadStream
(
'example.txt'
,
'utf8'
)
;
readableStream.
on
(
'data'
,
(
chunk
)
=>
{
console.
log
(
'Received chunk:'
,
chunk
)
;
readableStream.
destroy
(
)
;
// Destroy the stream
}
)
;