您的位置:首页 > Web前端 > Node.js

nodejs中的流

2017-11-03 19:32 141 查看
首先定义:流 在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')

以上就是木持原对读流,写流,还有读写流(毒瘤,血流,毒血瘤)的简单介绍。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nodejs 对象