In Node.js, streams are a way to efficiently handle reading from or writing to data sources, such as files. They allow us to process data in small, manageable chunks rather than loading the entire content into the memory. Streams help to work with large datasets or perform real-time processing, enhancing performance and reducing memory usage. To summarize, streams can be thought of as a continuous flow of data, where we can process information as it arrives, piece by piece, instead of waiting for the entire set to load.
writable.write()
method in Node.jsIn Node.js, there are readable as well as writable streams. In this Answer, we’ll look at writable streams. Specifically, we’ll learn to write data to the stream using the writable.write()
method. The writable.write()
method in Node.js is used to write data to a
The writable.write()
method in Node.js has the following syntax:
writable.write(chunk, encoding, callback);
The writable.write()
method takes the following parameters:
chunk
: This is the data to be written to the writable stream. It can be of type String
or Buffer
.
encoding
: This is the character encoding for the data. It is optional and often specified if the chunk
is a String
.
callback
: This is an optional callback function to be called when all the data has been successfully written to the writable stream.
Let’s look at an example where we build a system that continuously writes log data to a file. The data could be logs from a server, application, or user activity. We utilize the writable.write()
method to write data to a writable stream.
const fs = require('fs'); const writable = fs.createWriteStream('example.txt'); const dataChunks = ['Lorem ipsum', 'dolor sit', 'amet']; dataChunks.forEach((dataChunk, _) => { writable.write(dataChunk, 'utf-8', (err) => { if (err) { console.error('Error writing data: ', err); } else { console.log('Current data chunk: ', dataChunk); console.log(`Data chunk "${dataChunk}" has been written to the writable stream.`); console.log(""); } }); }); writable.end();
Let’s go through the above code line by line:
Line 1: We import the fs
module for file system operations.
Line 3: We create a writable stream to the example.txt
file, i.e., all content will be written inside the example.txt
file using the writable stream.
Line 5: We define an array of data chunks to be written to the writable stream.
Lines 7–17: We iterate over each chunk within the dataChunks
array. We write each data chunk to the writable stream using the utf-8
character encoding. We also pass a callback function that logs any errors on the console. Finally, if writing to the writable stream is successful, we log relevant success messages on the console.
Line 19: We close the writable stream.
Note: Once the data has been successfully written to the writable stream, you can view the contents of the
example.txt
file by executing the following command inside the terminal:cat example.txt
Attempt the quiz below to test your understanding of the writable.write()
method in Node.js.
Quiz!
What does the writable.write()
method do in Node.js?
Reads data from a writable stream
Writes data to a writable stream
Closes a writable stream
Pauses a writable stream
Free Resources