node.js学习笔记---stream

关于stream的相关基础知识

常用的包 through2 这是through 的描述>Easy way to create a Stream that is both readable and writable.

类比through2就是他的升级版,through2相关API如下
through2(optional,function(){},function(){});
//optional ture或者false
//第一个函数为处理函数,数据流入时触发,处理输入流 ;第二个函数在输入流输入完毕后触发
// 函数参数function (chunk, encoding, callback) {}.
// chunk就是一般意义上的buffer
//在函数中用this.push()来输出数据 紧接着next();来获取下一条chunk;

下面看一个完整的例子

var through=require("through2");
var tr=through(function(buf, _,next){
    this.push(buf.toString().toUpperCase());
    next();

});
process.stdin.pipe(tr).pipe(process.stdout);

具体就是酱紫

常用的包 concat-stream 字面意思就能想到是将chunk拼接为一个完整的stream输出

相关API

posted on 2016-03-01 22:39  liuestc  阅读(144)  评论(0编辑  收藏  举报