Analysis on Readable object _ Node. js in the Stream module of node. js

Source: Internet
Author: User
This article mainly introduces Node. the Readable object in the Stream module of js is Node. I have been reluctant to pull the node. js stream for reference by my friends who need the basic knowledge in getting started with js, because from the first time I saw it, I thought its design was really disgusting. However, there is no way. The Stream specification is not yet popular, and many things rely on the Stream of nodejs, therefore, I can only drag this nose nodejs Stream object with my nose and scalp.
Nodejs comes with a module called stream, which can be used to obtain a set of stream object constructor. Now I will only talk about the simplest stream. Readable.
In fact, almost all nodejs users have access to Readable instances, but they do not care too much at ordinary times. A typical example is that in the http module, we have req and res objects when processing each request. req is actually a Readable object. We can read the entity part of the HTTP request in the form of a stream on this req.
The question is, why is the http module designed here as a stream? You can also ask this question from another dimension: "If nodejs obtains the POST request content ?」. Those who know how to use the search engine can easily find the answer: Listen to the data event to collect data and combine the collected data in the end event. Yes, this is the solution to this problem. But why is it so designed? How nice is POST content that can be obtained directly like PHP? In fact, this design is advantageous. If the data we receive is illegal, I can immediately detect it, then respond and disconnect. This avoids unnecessary transmission costs. For example, when uploading an image, you may mistakenly select a large executable file. We do not need to wait until the file is completely uploaded, you only need several bytes in the header of a file to determine whether the file is an image. The stream design can be used to read the previous several bytes.
The data event and end event mentioned above are both Readable events, which indicate that the data is received and the data is received respectively. So we already know the usage of Readable, but many people do not know that it is a Readable object.
However, the above two events are only for Readable consumers. Internally, how does one push data to a Readable object so that Readable can trigger these events? Then it is the push method. The following is an example. It creates a Readable object, which will flow out of an incremental number (babel-node is used here)

Import stream from 'stream'; var r = new stream. readable; r. on ('data', data => {console. log (data + '') ;}); r. on ('end', data => {console. log ('end') ;}); r. _ read = () => {// console. log ('before read') ;}; void function callee (I) {if (I <10) {r. push (I + ''); // only strings or Buffre objects can be input} else {r. push (null); // when the input is null, it indicates that the stream transmission is complete and the end event is triggered} setTimeout (callee, 500, I + 1);} (0 );

If you carefully read the above Code, you will find a magical place. This Code overwrites the _ read method. What is this? In fact, I also think this is a pitfall, so this private naming style will not bother. Why do we have to override this method to implement it? If this method is not overwritten, an exception is thrown when push is called:

 Error: not implemented  at Readable._read (_stream_readable.js:464:22)  at Readable.read (_stream_readable.js:341:10)

These are the basic usage of the Readable object. But there are more pitfalls to step on. This article is just the simplest introduction to let everyone learn how to create a Readable object that can output data. As for some basic methods such as read, these are also unscientific designs.

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.