Node.js streams file Read and write operation detailed _node.js

Source: Internet
Author: User
Tags event listener readable

Node.js is inherently asynchronous and event-driven, and is ideal for handling I/O-related tasks. If you are dealing with I/O related operations in the application, you can take advantage of the stream in the Node.js. So let's take a look at the flow first, and understand how they simplify I/O operations.

What the stream is
a stream is a Unix pipeline that allows you to easily read data from a data source and then flow to another destination.
Simply put, the stream is not something special, it's just a eventemitter that implements some methods. According to the method it implements, the stream can become a readable stream (readable), a writable stream (writable), or a bidirectional flow (Duplex, both readable and writable).
A readable stream allows you to read data from a data source, while a writable stream allows you to write data to the destination.
If you've already used node.js, you're probably already in the flow.
For example, in a node.js HTTP server, request is a readable stream, and response is a writable stream.
You may also 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 flow. This article discusses both readable and writable streams, which are beyond the scope of this article and are not discussed.
Readable stream (readable Streams)
we can read data from a data source with a readable stream, which can be anything, such as a file in the system, a buffer in memory, or even another stream. Because streams are eventemitter, they send data with various events. We will use these events to make the flow work.

Reading data from the stream
the best way to read data from a stream is to listen for the date event and add a callback function. When data flows through it, the readable stream sends a data event, and the callback function fires. Look at the following code fragment:

 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 very beginning, the stream is not dynamic. When you add a data event listener, and a callback function, it becomes a stream dynamic. After that, it reads a small piece of data and passes it to your callback function.
The implementation of the stream determines the frequency at which the data event is triggered, for example, when the HTTP request triggers the data event when reading a few KB. When you read from a file, you may decide to trigger the data event when a row is read.
When there is no data to read (when the end of the file is read), the stream sends the end event. In the example above, we monitor this event and print out the data when we finish reading the file.
There is another way to read the stream, and you can just keep calling the Read () method in the stream instance before you read 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 the data from the internal buffer and returns NULL when there is no data to read.
Therefore, in the while loop we check that the read () returns null and terminates the loop when it returns NULL.
It should be noted that when we can read data from the stream, the readable event is triggered.
Set encoding
By default, what you read from the stream is the Buffer object. If you're going to read a string, it's not for you. Therefore, you can set the stream's encoding by calling Readable.setencoding () as in the following example:

 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 example above, we set the encoding of the stream to UTF8, the data is parsed into UTF8, and the chunk in the callback function is a string.
Pipe (piping)
Piping is a great mechanism, you don't need to manage the state of your flow to read data from the data source and then write to the destination. 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 contents of File1 to File2. Because pipe () will help you manage your data flow, you don't need to worry about the speed of the data flow. This makes pipe () very simple and easy to use.
It's important to note that pipe () will return to the destination stream, so you can easily link multiple streams together!
links (chaining)
Suppose you have an archive file that you want to unzip. There are many ways to accomplish this task. But the simplest way 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 Zlib.creategunzip () stream, which extracts 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, and here are some ways you need to know:
1.readable.pause () – This method pauses the flow of the stream. In other words, it will no longer trigger the data event.
2.readable.resume () – This method, contrary to the above, will allow the paused stream to flow back.
3.readable.unpipe () – This method will remove the destination. If a parameter is passed in, it will let the readable stream stop Liu Xiang from a particular destination, otherwise it will remove all destinations.

Writable flow (writable Streams)
A writable stream allows you to write data to a destination. Like a readable stream, these are also eventemitter, and they trigger different events. Let's take a look at the events and methods that are triggered in the writable stream.
Write Stream
To write data into a writable stream, you need to call the Write () method in a 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 simply reads the data from the input stream and writes it to the destination with write ().
This method returns a Boolean value to indicate whether the write succeeded. If the return is true that means write success, you can continue to write more data. If it is false, that means something wrong, and you can't continue writing now. A writable stream triggers a drain event to tell you that you can continue to write data.
After you finish writing the data
when you do not need to write the data, you can call the end () method to tell the stream that you have finished writing. Assuming Res is an HTTP response object, you usually send a response to the browser:
res.write (' Some data!! ');
Res.end ();
when End () is invoked, all data is written, and then the stream triggers a finish event. Note that after the end () is called, you cannot write data to the writable stream. For example, the following code will have an error:
res.write (' Some data!! ');
Res.end ();
Res.write (' Trying to write again '); Error!
Here are some important events related to the writable flow:
1.error– is triggered when a write or link error occurs
2.pipe– when a readable stream is linked to a writable stream, this event triggers
3.unpipe– is triggered when a readable stream calls Unpipe

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.