nodejs中的流

来源:互联网 发布:actor critic知乎 编辑:程序博客网 时间:2024/06/05 19:06
首先定义:流 在nodejs中流是一个对象
   nodejs官方写了一个核心模块stream【流】,来操作流

1.对于nodejs中的流,分为读流对象  写流对象 还有一种特殊的读写流

var stream = require('stream')//引入流对象


//创建读流对象
var sr = new stream.Readable
//创建写流对象
var sw = new stream.Writable //这里是没有e的

2.继承:stream继承自EventEmitter对象,因此读流和写流就拥有on 和 emit的方法


读流事件应用 on、emit  的例子:

sr.on('big',function(){

     console.log('ok')

})

 sr.emit('big')


写流应用  on、emit 的例子:

sw.on('write',function(){
       console.log('yes')
})

sw.emit('write')


以下就是读写流  Duplex
//读写流 也是继承自EventEmitter
var sd = new stream.Duplex//创建读写流
//读写流应用 on、emit
sd.on('duplex',function(){
    console.log('nice')
})

sd.emit('duplex')


以上就是木持原对读流,写流,还有读写流(毒瘤,血流,毒血瘤)的简单介绍。













原创粉丝点击