I/O
I/O is one of the core pieces that makes Node different from other frameworks. This section explores the APIs that provide nonblocking I/O in Node.
Streams
Many components in Node provide continuous
output or can process continuous input. To make these components act in
a consistent way, the stream API
provides an abstract interface for them. This API provides common
methods and properties that are available in specific implementations of
streams. Streams can be readable, writable, or both. All streams
are EventEmitter
instances, allowing them to emit events.
Readable streams
The readable stream API is a set of methods and events that provides
access to chunks of data as they are sent by an underlying data
source. Fundamentally, readable streams are about emitting data events. These events represent the
stream of data as a stream of events. To make this manageable, streams
have a number of features that allow you to configure how much data
you get and how fast.
The basic stream in Example 4-16 simply reads data from a file in chunks.
Every time a new chunk is made available, it is exposed to a callback
in the variable called data. In
this example, we simply log the data to the console. However, in real
use cases, you might either stream the data somewhere else or spool it
into bigger pieces before you work on it. In essence, the data event simply
provides access to the data, and you have to figure out what to do
with each chunk.
Example 4-16. Creating a readable file stream
var fs ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access