當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。