Refer to this Mytour article for further insights into what Node.js Streams are and how to write and read data into Streams.
Exploring Node.js Streams
Article Contents
1. What is Node.js Stream?
2. How many types of Streams are there in Node.js?
3. Reading data from Stream in Node.js
4. Writing data to Stream in Node.js
5. Concept of Piping Stream in Node.js
6. Concept of Chaining Stream in Node.js
1. What is Node.js Stream?
Node.js Streams are objects that help to read data from a source and write data to a destination.
2. How many types of Streams are there in Node.js?
There are 4 types of streams in Node.js, including:
- Readable: This stream is used for read operations.
- Writable: This stream is used for write operations.
- Duplex: This stream is used for both read and write operations.
- Transform: A type of Duplex stream, the output is based on the data you input.
Each stream is an EventEmitter variant, emitting different events at different times. Here are some common events used:
- Data: Triggered when data is available to be read.
- End: Triggered when there is no more data to be read.
- Error: Triggered when any error occurs during data writing or receiving.
- Finish: Triggered when all data in the system has been flushed.
3. Reading data from Stream in Node.js
Create a text file named input.txt with the following content:
Javatpoint is a one of the best online tutorial website to learn different technologies in a very easy and efficient manner.
Next, create a JavaScript file named main.js with the following code:
Execute the command node main.js in your terminal.
The output result should look like the example below:
As you can observe, a text file named output.txt is generated at the location where the input.txt and main.js files are saved. In this example, it is on the desktop screen.
Open the output.txt file, and on the screen, you will see the content as follows:
5. What is Piping Stream?
Piping is a mechanism that takes the output of one Stream and uses it as the input for another Stream. It's commonly employed to transfer data from one Stream to another, and there are no limitations to its Piping capabilities. Below is an example of piping, reading from one file and writing to another.
Create a JavaScript file named main.js, containing the following code:
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 the read and write operations
// read input.txt and write data to output.txt
readerStream.pipe(writerStream);
console.log('Program Ended');
Next, open the Node.js command prompt and run main.js:
node main.js
Output Result:
You can observe the creation of a text file named output.txt at the location where the input.txt file and main.js file are stored. In this example, it's on the desktop screen.
Open the output.txt file, and on the screen, you'll find the content as shown below:
6. Chaining Streams in Node.js
Chaining streams is a mechanism for stringing together multiple stream operations by connecting the output of one stream to another. It's often used in conjunction with piping. Here's an example of combining piping and chaining streams to compress a file and then decompress it.
Create a JavaScript file named main.js, containing the following code:
var fs = require('fs');
var zlib = require('zlib');
// Compress the file input.txt to input.txt.gz
fs.createReadStream('input.txt')
.pipe(zlib.createGzip())
.pipe(fs.createWriteStream('input.txt.gz'));
console.log('File Compressed.');
Next, open the Node.js command prompt and run main.js:
node main.js
The returned result looks like:
You will notice the input.txt file compressed, and a new file is created with the name input.txt.gz at the same location as the current file.
To decompress the file, input the following code into the main.js file:
var fs = require('fs');
var zlib = require('zlib');
// Decompress the file input.txt.gz to input.txt
fs.createReadStream('input.txt.gz')
.pipe(zlib.createGunzip())
.pipe(fs.createWriteStream('input.txt'));
console.log('File Decompressed.');
Next, open the Node.js command prompt and run main.js:
node main.js
The returned result looks like:
In this article, Mytour has just introduced you to what streams are in Node.js. Additionally, readers can explore more articles on Learning Node.js available on Mytour to delve deeper into Node.js installation and understanding Callback in Node.js.
