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


Node.js zlib.createBrotliCompress()用法及代碼示例


zlib.createBrotliCompress()方法是Zlib模塊的內置應用程序編程接口,用於創建BrotliCompress對象。

用法:

zlib.createBrotliCompress( options )

參數:此方法接受單個參數選項,這是一個包含zlib選項的可選參數。



返回值:它返回一個新的BrotliCompress對象。

以下示例說明了Node.js中zlib.createBrotliCompress()方法的使用:

範例1:

// Node.js program to demonstrate the      
// createBrotliCompress() method 
  
// Including zlib and fs module 
const zlib = require("zlib"); 
const fs = require('fs'); 
  
// Creating readable Stream 
const inp = fs.createReadStream('input.txt'); 
  
// Creating writable stream 
const out = fs.createWriteStream('input.txt.gz'); 
  
// Calling createBrotliCompress method 
const brot = zlib.createBrotliCompress(); 
  
// Piping 
inp.pipe(brot).pipe(out); 
console.log("Program Completed!");

輸出:

Program Completed!

範例2:

// Node.js program to demonstrate the      
// createBrotliCompress() method 
  
// Including zlib and fs module 
const zlib = require("zlib"); 
const fs = require('fs'); 
  
// Creating readable Stream 
const inp = fs.createReadStream('input.txt'); 
  
// Creating writable stream 
const out = fs.createWriteStream('input.txt.gz'); 
  
// Calling createBrotliCompress method 
const brot = zlib.createBrotliCompress(); 
  
// Piping 
inp.pipe(out).pipe(brot); 
console.log("Program Completed!");

輸出:

Error [ERR_STREAM_CANNOT_PIPE]:Cannot pipe, not readable
    at WriteStream.Writable.pipe (_stream_writable.js:243:24)
    at /home/runner/CorruptFlawedSoftwaresuite/index.js:19:15
    at Script.runInContext (vm.js:133:20)
    at Object. (/run_dir/interp.js:156:20)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

在此,配管未按正確的順序進行,因此會引發錯誤。

參考: https://nodejs.org/api/zlib.html#zlib_zlib_createbrotlicompress_options




相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | zlib.createBrotliCompress() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。