首页 > 代码库 > stream

stream

1.四个流类型。 读、写、可读写和可改变四个流。

2.看一个例子:

var destroy = require(‘destroy‘)
var http = require(‘http‘)
var onFinished = require(‘on-finished‘)
 
http.createServer(function onRequest(req, res) {
  var stream = fs.createReadStream(‘package.json‘)
  //res为一个可写流,pipe是把源流输出到目标可写流(writable)
  stream.pipe(res)
  //如果完成,触发销毁这个流
  onFinished(res, function (err) {
    destroy(stream)
  })
})

 3.stream类的原型

var Stream = require(‘stream‘);
//Stream { pipe: [Function] },  只有一个pipe方法。
console.log(Stream.prototype);

 4.可读流的方法

var Stream = require(‘stream‘);
var readableStream = Stream.Readable;
/*
    Readable {
      push: [Function],
      unshift: [Function],
      isPaused: [Function],
      setEncoding: [Function],
      read: [Function],
      _read: [Function],
      pipe: [Function],
      unpipe: [Function],
      on: [Function],
      addListener: [Function],
      resume: [Function],
      pause: [Function],
      wrap: [Function] 
    }
 */
console.log(readableStream.prototype);

 

stream