Native Nodejs Learning Note 2

Source: Internet
Author: User

One of the benefits of learning flow in this chapter is to reduce the callback hell of various asynchronous IO. IO operations throughout our various operations, such as database read and write, file read and write, file conversion compression ... Other than that, like the first section, we're going to return an HTML file to the browser, which involves IO operations.

A page, if broken down by version, may be cut into chunks for different people, using the FS method of the asynchronous Io method, which may be written like this:

Fs.asyncxxx (function (err,data) {   fs.asyncxxx (function (err,data) {      fs.asyncxxx (function (err,data)         { Fs.asyncxxx (function (err,data) {})})}   )

If you are using a stream, write this

Readstream.pipe (Transformstream). Pipe (Writestream)
A.pipe (b). pipe (c). PIPE (d)//equivalent to A.pipe (b); B.pipe (c); C.pipe (d);

Is this similar to the pipeline in UNIX?! There is no mistake, it is the inspiration is this!

A | B | C | D

Also, without a stream, if you read a large file, you need to put it all in memory, which can affect performance. Use the stream without worrying about it.

The Nodejs bottom one provides 4 streams, readable, writable, Duplex, and transform streams. If you do not yet meet your needs, you can extend the base class that inherits its own streams. (e.g. Util.inherits (Mytransform, Transform); )

Usage Scenarios Class Methods that need to be overridden
Read-only Readable _read
Write only Writable _write
Duplex Duplex _read, _write
The operation is written to the data and then the result is read Transform _transform, _flush

Having said so much, we haven't seen a stream yet. Let's take some examples.

Readable stream:

Aaa.jsvar fs = require (' FS '), var readstream = Fs.createreadstream (' myfile.txt ');//write a few lines of readstream.pipe ( Process.stdout);

The text is read directly above and then output to the console. We can also encrypt the output

var crypto = require (' crypto '); var fs = require (' FS '); var readstream = Fs.createreadstream (' myfile.txt '); var hash = Crypto . CreateHash (' SHA1 '); Readstream  . On (' Data ', function (chunk) {    hash.update (chunk);  })  . On (' End ', function () {    console.log (hash.digest (' hex '));  });

Output a bunch of passwords you can't read:

The FS module also has a way to create a readable stream:

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);});

Let's look at the various events of the readable stream

var fs = require (' FS '), var readstream = Fs.createreadstream (' myfile.txt '), Readstream  . On (' Data ', function (chunk) {  Console.log ("Emit data")    Console.log (chunk.tostring (' UTF8 ')  })  . On (' End ', function () {    Console.log ("Emit End");  })  . On (' Close ', function () {    Console.log ("Emit close");  })  . On ("Readable", function () {     console.log ("Emit readable")  })  . On ("Error", function (e) {     Console.log ("Emit error")  })

Look again at how to rewrite the _read method:

var readable = require (' stream '). Readable;var util = require (' util '); function countingobjectstream (length, options) {if (!) (  This instanceof Countingobjectstream) {return new Countingobjectstream (length, options); } if (!options) options = {}; Ensure object Options.objectmode = true;  Forcing object mode Readable.call (this, options);  This.lentocount = length;  How far to count This.index = 0; To track our count}util.inherits (Countingobjectstream, readable);  Countingobjectstream.prototype._read = function () {this.index + = 1; if (This.index > This.lentocount) {return this.push (null);//Done, return}//pushing number, but could is any Non-null obj This.push (this.index);};/ /consume this stream and output to stdout//coercing it to a stringvar readstream = new Countingobjectstream; readstre    Am. On (' readable ', function () {var obj;    while (null!== (obj = Readstream.read ())) {console.log (obj); }  });

The readable has an optional hash parameter with three configuration items:

    • Highwatermark {Number} stops the maximum amount of bytes that the internal buffer can hold before reading from the underlying resource. The default is 16KB, which is 16 for objectmode flow
    • encoding {string} if given, Buffer will be decoded to the given encoded string. Default is NULL
    • Objectmode {Boolean} whether the stream should behave as an object's stream. That means Stream.read (n) returns a single object instead of a Buffer of size n

The first two configurations are easy to understand, so let's look at the third one:

var stream = require (' stream '), var util = require (' util '), function Stringifystream () {    stream. Transform.call (this);    This._readablestate.objectmode = false;    This._writablestate.objectmode = true;} Util.inherits (Stringifystream, stream. Transform); Stringifystream.prototype._transform = function (obj, encoding, CB) {    This.push (json.stringify (obj));    CB ();}; var json = require (__dirname + ' Test.json '); Console.log (JSON)//This is an object var rs = new stream. Readable ({objectmode:true}); Rs.push (JSON); Rs.push (null); Rs.pipe (new Stringifystream ()). pipe (Process.stdout);

Here's Test.json.

{"A": "2", "B": {      "xxx": 1,   "yyy": false   }}
Writable stream

The constructor has an optional configuration object, by default the encoding is UTF8

var fs = require (' FS '); var wstream = Fs.createwritestream (' myOutput.txt '); Wstream.write (' Hello world!\n '); Wstream.write (' another line\n '); Wstream.end ();

We can change that code.

var fs = require (' FS '); var wstream = Fs.createwritestream (' myOutput.txt '); Wstream.write (' Hello world!\n '); Wstream.write (' another line\n '); Wstream.end ();

Output binary file

var crypto = require (' crypto '); var fs = require (' FS '); var Wstream = Fs.createwritestream (' mybinaryfile ');//creates random buffer of bytesvar buffer = crypto.randombytes (100); Wstream.write (buffer);//Create another buffer of bytes and writewstream.write (crypto.randombytes (100)); Wstream.end (); 
Http://www.sandersdenardi.com/readable-writable-transform-streams-node/http://www.slideshare.net/shigeki_ohtsu /stream2-kihonhttps://cnodejs.org/topic/513ef6cc069911196d0c90a6http://nodeapi.ucdok.com/#/api/stream.htmlhttp ://www.it165.net/pro/html/201406/15924.htmlhttp://calv.info/an-introduction-to-nodes-new-streams/http:// codewinds.com/blog/2013-08-20-nodejs-transform-streams.htmlhttp://stackoverflow.com/questions/20317759/ implementing-a-buffered-transform-streamhttp://maxogden.com/node-streams.htmlhttp://codewinds.com/blog/ 2013-08-19-nodejs-writable-streams.html, Http://codewinds.com/blog/2013-08-04-nodejs-readable-streams.htmlhttp ://codewinds.com/blog/2013-08-20-nodejs-transform-streams.html

Native Nodejs Learning Note 2

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.