zlib.brotliCompress()方法是Zlib模块的内置应用程序编程接口,用于压缩大量数据。
用法:
zlib.brotliCompress( buffer, options, callback )
参数:此方法接受上述和以下所述的三个参数:
- buffer:它可以是Buffer,TypedArray,DataView,ArrayBuffer和字符串的类型。
 - options:它是一个可选参数,其中包含zlib选项。
 - callback:它包含回调函数。
 
返回值:压缩后返回数据块。
以下示例说明了Node.js中zlib.brotliCompress()方法的使用:
范例1:
// Node.js program to demonstrate the      
// brotliCompress() method 
  
// Including zlib module 
const zlib = require("zlib"); 
  
// Declaring input and assigning 
// it a value string 
var input = "Computer Science"; 
  
// Calling brotliCompress method 
zlib.brotliCompress(input, (err, buffer) => { 
  
        console.log(buffer.toString('base64')); 
});输出:
Gw8A+CUAqhBDSpVv9iA=
范例2:
// Node.js program to demonstrate the      
// brotliCompress() method 
  
// Including zlib module 
const zlib = require("zlib"); 
  
// Declaring input and assigning 
// it a value string 
var input = "Computer Science"; 
  
// Calling brotliCompress method 
zlib.brotliCompress(input, (err, buffer) => { 
  if(!err) { 
      console.log(buffer.toString('hex')); 
  } 
  else { 
      console.log(err); 
  } 
});输出:
1b0f00f82500aa10434a956ff620
参考: https://nodejs.org/api/zlib.html#zlib_zlib_brotlicompress_buffer_options_callback
相关用法
- Node.js GM blur()用法及代码示例
 - Node.js GM drawRectangle()用法及代码示例
 - Node.js GM sharpen()用法及代码示例
 - Node.js GM drawPolyline()用法及代码示例
 - Node.js GM drawEllipse()用法及代码示例
 - Node.js GM drawCircle()用法及代码示例
 - Node.js GM drawPolygon()用法及代码示例
 
注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Node.js | zlib.brotliCompress() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
