当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


node.js Stream readable.unpipe()用法及代码示例


可读流中的readable.unpipe()方法用于分离使用stream.pipe()方法时先前附加的可写流。

用法:

readable.unpipe( destination )

参数:此方法接受单个参数目标,该目标是要分离的可写流的目标。


返回值:它返回stream.Writable,即目的地。

下面的示例说明在Node.js中使用visible.unpipe()方法:

范例1:

// Node.js program to demonstrate the      
// readable.unpipe() method 
   
// Accessing fs module 
const fs = require('fs'); 
  
// Constructing readable stream 
const readable = fs.createReadStream("input.text"); 
  
// Constructing writable Stream 
const writable = fs.createWriteStream("output.text"); 
  
// Calling pipe method 
readable.pipe(writable); 
  
// Calling unpipe method 
readable.unpipe(writable); 
console.log("done");

输出:

done

范例2:

// Node.js program to demonstrate the      
// readable.unpipe() method 
   
// Accessing fs module 
const fs = require('fs'); 
  
// Constructing readable stream 
const readable = fs.createReadStream("input.text"); 
  
// Constructing writable Stream 
const writable = fs.createWriteStream("output.text"); 
  
// All the data from readable goes into 'output.text', 
// for only two seconds. 
readable.pipe(writable); 
setTimeout(() => { 
  console.log('Stop writing to output.text.'); 
    
  // Calling unpipe method 
  readable.unpipe(writable); 
  console.log('close the file stream.'); 
    
  //Calling end method 
  writable.end(); 
}, 2000); 
console.log("done");

输出:

done
Stop writing to output.text.
close the file stream.

参考: https://nodejs.org/api/stream.html#stream_readable_unpipe_destination



相关用法


注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Node.js | Stream readable.unpipe() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。