原生nodejs 学习笔记2

本章节学习流, 流的一个好处在于减少各种异步IO的回调地狱。IO操作遍及我们各种操作,比如数据库读写,文件读写, 文件转换压缩……别的不说,比如第一节,我们要将一个HTML文件返回浏览器,就涉及IO操作。

一个页面,如果按版本划分功能,可能切成一块块给不同的人做,使用fs方法的异步IO方法,可能是这样写的:

fs.asyncXXX(function(err,data){
   fs.asyncXXX(function(err,data){
      fs.asyncXXX(function(err,data){
         fs.asyncXXX(function(err,data){

         })
      })
   })
})

如果使用流,则是这样写

readStream.pipe(transformStream).pipe(writeStream)

a.pipe(b).pipe(c).pipe(d)
//相当于
a.pipe(b);
b.pipe(c);
c.pipe(d);

这是不是与Unix中的管道很相似呢?!无错,它的灵感就在于这!

a | b | c | d

此外,不使用流,如果读出一个很大的文件,则需要将它整个装进内存中,这会很影响性能。使用了流就不用担心这个。

nodejs底层一个提供了4个流, Readable 流、Writable 流、Duplex 流和Transform 流。如果还不能满足你的需求,可以自己继承其个流的基类进行扩展。(例如util.inherits(MyTransform, Transform); )

使用情景 需要重写的方法
只读 Readable _read
只写 Writable _write
双工 Duplex _read, _write
操作被写入数据,然后读出结果 Transform _transform, _flush

说了这么多,我们还没有瞧见一个流。我们来一些例子吧.

可读流:

//aaa.js
var fs = require(‘fs‘);
var readStream = fs.createReadStream(‘myfile.txt‘);//里面乱写几行
readStream.pipe(process.stdout);

上面是直接读文本的,然后输出到控制台。我们也可以加密一下输出

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

输出一大堆看不懂的密码:

fs模块也有创建可读流的方法:

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

我们再看一下可读流的各种事件

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

再看一下如何重写_read方法:

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 be any non-null obj
  this.push(this.index);
};

// consume this stream and output to stdout
// coercing it to a string
var readStream = new CountingObjectStream(10);
readStream
  .on(‘readable‘, function () {
    var obj;
    while (null !== (obj = readStream.read())) {
      console.log(obj);
    }
  });

Readable有一个可选的hash参数里,里面有三个配置项:

  • highWaterMark {Number} 停止从底层资源读取前内部缓冲区最多能存放的字节数。缺省为 16kb,对于 objectMode 流则是 16
  • encoding {String} 若给出,则 Buffer 会被解码成所给编码的字符串。缺省为 null
  • objectMode {Boolean} 该流是否应该表现为对象的流。意思是说 stream.read(n) 返回一个单独的对象,而不是大小为 n 的 Buffer

前两个配置项比较易懂,我们看第三个:

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) //这是一个对象
var rs = new stream.Readable({ objectMode: true });
rs.push(json);
rs.push(null);

rs.pipe(new StringifyStream()).pipe(process.stdout);

下面是test.json

{
"a":"2",
"b":{
      "xxx": 1,
	   "yyy": false
   }
}

可写流

构造器有一个可选配置对象,默认是编码是utf8

var fs = require(‘fs‘);
var wstream = fs.createWriteStream(‘myOutput.txt‘);
wstream.write(‘Hello world!\n‘);
wstream.write(‘Another line\n‘);
wstream.end();

我们可以这样改编码

var fs = require(‘fs‘);
var wstream = fs.createWriteStream(‘myOutput.txt‘);
wstream.write(‘Hello world!\n‘);
wstream.write(‘Another line\n‘);
wstream.end();

输出二进制文件

var crypto = require(‘crypto‘);
var fs = require(‘fs‘);
var wstream = fs.createWriteStream(‘myBinaryFile‘);
// creates random Buffer of 100 bytes
var buffer = crypto.randomBytes(100);
wstream.write(buffer);
// create another Buffer of 100 bytes and write
wstream.write(crypto.randomBytes(100));
wstream.end();

http://www.sandersdenardi.com/readable-writable-transform-streams-node/
http://www.slideshare.net/shigeki_ohtsu/stream2-kihon
https://cnodejs.org/topic/513ef6cc069911196d0c90a6
http://nodeapi.ucdok.com/#/api/stream.html

http://www.it165.net/pro/html/201406/15924.html
http://calv.info/an-introduction-to-nodes-new-streams/
http://codewinds.com/blog/2013-08-20-nodejs-transform-streams.html

http://stackoverflow.com/questions/20317759/implementing-a-buffered-transform-stream
http://maxogden.com/node-streams.html
http://codewinds.com/blog/2013-08-19-nodejs-writable-streams.html、
http://codewinds.com/blog/2013-08-04-nodejs-readable-streams.html
http://codewinds.com/blog/2013-08-20-nodejs-transform-streams.html

时间: 2024-08-02 00:20:08

原生nodejs 学习笔记2的相关文章

原生nodejs 学习笔记1

网上许多nodejs教程或书藉都是教你调用第三方模块来编写nodejs应用的,虽然这是非常便捷的,但是封装太厚,你基本一点东西还是没有学到.人家的模块,人家想怎么改就行,可以下一版本就改了接口,你的应用就完蛋了.比如说google,他就爱干这种事情.因此我们还得老老实实学习底层API吧. 本节首先教大家跑起一个页面吧. 我在以前就写一篇相关的, node.js 一个简单的页面输出,大家可以先预习一下. 一般来说,大家都是从这样一个例子入门 var http = require("http&quo

Nodejs学习笔记(八)--- Node.js + Express 实现上传文件功能(felixge/node-formidable)

目录 前言 formidable简介 创建项目并安装formidable 实现上传功能 运行结果 部分疑惑解析 写在之后 前言 前面讲了一个构建网站的示例,这次在此基础上再说说web的常规功能----文件上传,示例以一个上传图片的功能为例子 上传功能命名用formidable实现,示例很简单! PS:最近比较忙,距上一次更新已经比较久了^_^! formidable简介 nodejs原生实现上传还是比较麻烦,有兴趣的自已去参考一下网上有网友写的代码 这里选择了formidable,也是githu

nodejs学习笔记(基于v7.4.0)

nodejs学习笔记 一.buffer: 全局对象(单例   Buffer.from   Buffer.alloc   Buffer.allocUnsafe ) 编码类型 描述 ascii 仅仅用于7位ascall数据编码,速度快,如果设置了将会剥离高位 utf8 多字节的编码的Unicode字符,网页文档大部分默认都为它. utf16le 小端编码的Unicode字符,2或者4个字节 ucs2 utf16le的别名 base64 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之

nodejs学习笔记_nodejs和PHP在基础架构上的差别--共享状态的并发

绝大多数对于Node.js的讨论都把关注点放在了处理高并发能力上,做开发的时候一定要明确node内部做出的权衡,以及node应用性能好的原因. node 为javascript引入了一个复杂的概念,:共享状态的并发. node採用一个长期执行的进程 而php在apache中会产生多个进程 例如以下图所看到的: 代码验证: PHP: <?php $i = 0; $i++; echo $i nodejs: var http = require('http'); var i=0; http.creat

nodejs学习笔记之安装、入门

由于项目需要,最近开始学习nodejs.在学习过程中,记录一些必要的操作和应该注意的点. 首先是如何安装nodejs环境?(我用的是windows 7环境,所以主要是windows 7的例子.如果想看linux下的安装可以参考http://www.cnblogs.com/meteoric_cry/archive/2013/01/04/2844481.html) 1. nodejs提供了一些安装程序,可以去官网(http://nodejs.org/download/)按照自己的机器进行下载,下载完

Nodejs学习笔记(三)——一张图看懂Nodejs建站

前言:一条线,竖着放,如果做不到精进至深,那就旋转90°,至少也图个幅度宽广. 通俗解释上面的胡言乱语:还没学会爬,就学起走了?! 继上篇<Nodejs学习笔记(二)——Eclipse中运行调试Nodejs>之后,代码编写环境就从Sublime转战到Eclipse下,感觉顺手多了.于是就跟着Scott老师学起了Nodejs建站的课程(推荐大家点进去看看),踏上了未爬先走的路子. 作为一个白里透白的小白来说,今天主要记录下如何用Nodejs搭建一个小小的网站,以及自己对于这种Nodejs建站的运

NodeJS学习笔记(一)——搭建开发框架Express,实现Web网站登录验证

JS是脚本语言,脚本语言都需要一个解析器才能运行.对于写在HTML页面里的JS,浏览器充当了解析器的角色.而对于需要独立运行的JS,NodeJS就是一个解析器.每一种解析器都是一个运行环境,不但允许JS定义各种数据结构,进行各种计算,还允许JS使用运行环境提供的内置对象和方法做一些事情.例如运行在浏览器中的JS的用途是操作DOM,浏览器就提供了document之类的内置对象.而运行在NodeJS中的JS的用途是操作磁盘文件或搭建HTTP服务器,NodeJS就相应提供了fs.http等内置对象.E

nodejs学习笔记_nodejs和PHP在基础架构上的区别--共享状态的并发

绝大多数对于Node.js的讨论都把关注点放在了处理高并发能力上,做开发的时候一定要明白node内部做出的权衡,以及node应用性能好的原因. node 为javascript引入了一个复杂的概念,:共享状态的并发. node采用一个长期运行的进程 而php在apache中会产生多个进程 如下图所示: 代码验证: PHP: <?php $i = 0; $i++; echo $i nodejs: var http = require('http'); var i=0; http.createSer

原生js学习笔记2

知识点: 1:关于this指向问题,如果有函数a(),直接a()那么this指向window,new a()指向函数本身. 2:关于null和undefined.两者如果用“==”则认为两者是相等的,要使用严格相等符号===来区分.typeof null --->object,type of undefined ---->undefined,   在希望值是bool值的地方,它们的值都是假值.它们都不包含任何属性和方法.使用“.”,"[]"来存取这两个值都会产生一个类型错误