Detailed description of Node. js Streams file read and write operations, node. jsstreams

Source: Internet
Author: User

Detailed description of Node. js Streams file read and write operations, node. jsstreams

Node. js is inherently asynchronous and event-driven, and is very suitable for processing I/O-related tasks. If you are dealing with I/O-related operations in the application, you can use stream in Node. js ). Therefore, let's take a look at the stream and understand how they simplify I/O operations.

What is a stream?
A stream is a unix pipeline that allows you to easily read data from the data source and then flow to another destination.
Simply put, a stream is nothing special, but an EventEmitter that implements some methods. Based on its implementation method, the stream can be converted into a Readable stream (Readable), a Writable stream (Writable), or a bidirectional stream (Duplex, both Readable and Writable ).
Readable streams allow you to read data from a data source, while writable streams allow you to write data to the destination.
If you have already used Node. js, you may have encountered an over-stream.
For example, in an HTTP server of Node. js, request is a readable stream and response is a writable stream.
You may have used the fs module, which can help you process readable and writable streams.
Now let you learn some basics and understand different types of streams. This article will discuss readable and writable streams. Two-way streams are beyond the scope of this article. We will not discuss them.
Readable Streams)
We can use a readable stream to read data from a data source. This data source can be anything, such as a file in the system, buffer in the memory, or even other streams. Because streams are EventEmitter, they send data using various events. We will use these events to make the stream work.

Read data from the stream
The best way to read data from a stream is to listen to data Events and add a callback function. When there is a data stream, the readable stream will send a data event, and the callback function will be triggered. Take a look at the following code snippet:

 var fs = require('fs');var readableStream = fs.createReadStream('file.txt');var data = '';var readableStream.on('data', function(chunk){ data += chunk;});readableStream.on('end', function(){ console.log(data);}); 

Fs. createReadStream will give you a readable stream.
At the beginning, the stream was not dynamic. When you add a data event listener and a callback function, it becomes a stream. After that, it reads a small piece of data and transmits it to your callback function.
The real-time stream determines the trigger frequency of data events. For example, HTTP requests trigger data events when reading several KB of data. When you read data from a file, you may decide to trigger the data event when a row is read.
When no data is readable (when the end of the file is read), the stream will send an end event. In the above example, we listened to this event and printed the data when reading the file.
There is another way to read the stream. You only need to call the read () method in the stream instance before reading the end of the file.

 var fs = require('fs');var readableStream = fs.createReadStream('file.txt');var data = '';var chunk;readableStream.on('readable', function(){ while ((chunk = readableStream.read()) != null) { data += chunk; }});readableStream.on('end', function(){ console.log(data);});

The read () method reads data from the internal buffer. If no data is readable, it returns null.
Therefore, in the while LOOP, we check whether read () returns null. When it returns null, the loop is terminated.
It should be noted that when we can read data from the stream, the readable event will be triggered.
Set Encoding
By default, you read the Buffer object from the stream. This is not suitable for you if you want to read strings. Therefore, you can call Readable. setEncoding () as shown in the following example to set the stream encoding:

 var fs = require('fs');var readableStream = fs.createReadStream('file.txt');var data = '';readableStream.setEncoding('utf8');readableStream.on('data', function(chunk){ data += chunk;});readableStream.on('end', function(){ console.log(data);});

In the above example, we set the stream encoding to utf8, and the data will be parsed to utf8, And the chunk in the callback function will be a string.
Pipeline)
Pipeline is a great mechanism. You can read data from the data source and write it to the destination without having to manage the stream status on your own. Let's take a look at the following example:

 var fs = require('fs');var readableStream = fs.createReadStream('file1.txt');var writableStream = fs.createWriteStream('file2.txt');readableStream.pipe(writableStream);

The above example uses the pipe () method to write the content of file1 to file2. Because pipe () helps you manage data streams, you do not need to worry about the speed of data streams. This makes pipe () very simple and easy to use.
It should be noted that pipe () will return the stream of the destination, so you can easily link Multiple Streams!
Link (Chaining)
Suppose there is an archive file and you want to decompress it. There are many ways to complete this task. However, the most concise method is to use pipelines and links:

 var fs = require('fs');var zlib = require('zlib');fs.createReadStream('input.txt.gz') .pipe(zlib.createGunzip()) .pipe(fs.createWriteStream('output.txt'));

First, we create a readable stream through input.txt.gz, and then let it stream the zlib. createGunzip () stream, which will decompress the content. Finally, we add a writable stream to write the extracted content to another file.
Other methods
We have discussed some important concepts in the readable stream. Here are some methods you need to know:
1. Readable. pause ()-This method will suspend the flow of the stream. In other words, it will not trigger data events again.
2. Readable. resume ()-This method is opposite to the above method and will resume the paused stream.
3. Readable. unpipe ()-This method will remove the destination. If a parameter is input, it will stop the readable stream from a specific destination of Liu Xiang. Otherwise, it will remove all destinations.

Writable Streams)
The writable stream allows you to write data to the destination. Like a readable stream, these are also EventEmitter, which also trigger different events. Let's take a look at the events and methods that will be triggered in the writable stream.
Write stream 
To write data to a writable stream, you need to call the write () method in the writable stream instance to see the following example:

 var fs = require('fs');var readableStream = fs.createReadStream('file1.txt');var writableStream = fs.createWriteStream('file2.txt');readableStream.setEncoding('utf8');readableStream.on('data', function(chunk){ writableStream.write('chunk');});

The code above is very simple. It only reads data from the input stream and writes it to the destination using write.
This method returns a Boolean value indicating whether the write is successful. If true is returned, the write operation is successful. You can continue to write more data. If it is false, it indicates that an error has occurred and you cannot continue writing the data. A writable stream triggers a drain event to tell you that you can continue writing data.
After writing data
When you do not need to write data, you can call the end () method to tell the stream that the data has been written. If res is an HTTP response object, you usually send a response to the browser:
Res. write ('some Data !! ');
Res. end ();
When end () is called, all data is written, and the stream triggers a finish event. Note that after end () is called, you cannot write data into the writable stream. For example, the following code reports an error:
Res. write ('some Data !! ');
Res. end ();
Res. write ('trying to write again '); // Error!
Here are some important events related to writable streams:
1. error-triggered when a write or link error occurs
2. pipe-this event is triggered when a readable stream is linked to a writable stream.
3. unpipe-triggered when the readable stream calls unpipe

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.