Hướng dẫn nodejs gzip - nodejs gzip

Cách tiếp cận sau đây bao gồm cách nén với nén GZIP trong Node.js. Chúng ta có thể sử dụng mô -đun nén để nén với nén GZIP. GZIP là nén được sử dụng rộng rãi nhất, đặc biệt là cho các tương tác máy chủ và máy khách.

Nén giúp giảm lượng dữ liệu có thể tải xuống trong ứng dụng NodeJS của chúng tôi và nó dẫn đến sự cải thiện trong hiệu suất của ứng dụng. Quá trình nén này có thể giảm kích thước tải trọng đáng kể trên 70%. helps in decreasing the amount of downloadable data in our NodeJS application, and it leads to an improvement in the performance of the application. This compression process can reduce the payload size dramatically above 70%.

Hai ví dụ sau đây bao gồm cách nén hữu ích vì nó làm giảm kích thước tải trọng như có thể nhìn thấy rõ trong ví dụ 2 so với ví dụ 1.

Ví dụ 1: Sử dụng mà không nén:

Bước 1: Tạo ứng dụng NodeJS trống với lệnh sau:Create an empty NodeJS application with the following command:

mkdir Project 
cd Project
npm init -y

Bước 2: Cài đặt mô -đun ExpressJS bằng lệnh sau: Install the ExpressJS module using the following command:

npm i express --save

Bước 3: Tạo một tệp trên thư mục gốc của dự án và tên index.js và viết mã sau. & NBSP;Create a file on your project’s root directory and name index.js and write the following code. 

index.js

const express = require(

npm i express --save
0
npm i express --save
1

npm i express --save
2

npm i express --save
3
npm i express --save
4
npm i express --save
5

npm i express --save
6
npm i express --save
7
npm i express --save
8

npm i express --save
9
node index.js
0

node index.js
1

node index.js
2
node index.js
3
node index.js
4

npm i express --save
9
node index.js
6
node index.js
7
npm i express --save
1

node index.js
1

Chạy tệp index.js bằng lệnh sau:index.js file using the following command:

node index.js

Mở trình duyệt và truy cập URL http: // localhost: 8080/, không có nén, phản hồi được máy chủ trả về sẽ nặng khoảng 16 kb, như được hiển thị bên dưới:http://localhost:8080/, Without compression, the response returned by the server will weigh around 16 kB, as shown below:

Hướng dẫn nodejs gzip - nodejs gzip

Ví dụ 2: Sử dụng nén GZIP:

Thêm phần mềm trung gian nén vào tệp index.js của ứng dụng NodeJS của bạn. Điều này sẽ cho phép GZIP giúp các phản hồi HTTP của bạn nhỏ hơn.index.js file of your NodeJS application. This will enable GZIP which makes your HTTP responses smaller.

Bước 1: Tạo ứng dụng NodeJS trống với lệnh sau:Create an empty NodeJS application with the following command:

mkdir Project
cd Project
npm init -y

Bước 2: Cài đặt mô -đun ExpressJS bằng lệnh sau: Install the compression and ExpressJS module using the following command:

npm install compression --save
npm install express --save

Bước 3: Tạo một tệp trên thư mục gốc của dự án và tên index.js và viết mã sau. & NBSP;Create a file on your project’s root directory and name index.js and write the following code. 

index.js

mkdir Project
cd Project
npm init -y
0
mkdir Project
cd Project
npm init -y
1
npm i express --save
1

const express = require(

npm i express --save
0
npm i express --save
1

npm i express --save
2

mkdir Project
cd Project
npm init -y
7

npm i express --save
3
npm i express --save
4
npm i express --save
5

npm i express --save
6
npm i express --save
7
npm i express --save
8

npm i express --save
9
node index.js
0

node index.js
1

node index.js
2
node index.js
3
node index.js
4

npm i express --save
9
node index.js
6
node index.js
7
npm i express --save
1

node index.js
1

Chạy tệp index.js bằng lệnh sau:index.js file using the following command:

node index.js

Mở trình duyệt và truy cập URL http: // localhost: 8080/, không có nén, phản hồi được máy chủ trả về sẽ nặng khoảng 16 kb, như được hiển thị bên dưới:http://localhost:8080/, Now, if compression is turn on the response returned by the server will weigh around 371 bytes, as shown below:

Ví dụ 2: Sử dụng nén GZIP:


Code ví dụ Node.js nén, giải nén file – module Zlib

(Xem thêm: Hướng dẫn Node.js)

Module sử dụng

Để thực hiện nén/giải nén file (zip/unzip) trong node.js ta sử dụng module

node index.js
5

zlib là module có sẵn khi cài node.js, để include zlib vào project ta dùng method

var zlib = require('zlib');

Nén file (zip)

Dưới đây là code ví dụ nén file

node index.js
6 thành file
node index.js
7

var zlib = require('zlib');
var fs = require('fs');

var gzip = zlib.createGzip();
var r = fs.createReadStream('hello.txt');
var w = fs.createWriteStream('hello.txt.gzip');
r.pipe(gzip).pipe(w);

console.log('done');

Đầu vào của method 

node index.js
8 và 
node index.js
9 sẽ là url của file được dùng để nén và url của file kết quả sau khi được nén

Giải nén file (unzip)

Đây là code ví dụ giải nén file

node index.js
7 thành file
var zlib = require('zlib');
1.

var zlib = require('zlib');
var fs = require('fs');

var unzip = zlib.createUnzip();

var read = fs.createReadStream('hello.txt.gz');
var write = fs.createWriteStream('unzip.txt');

read.pipe(unzip).pipe(write);	
console.log("unZipped Successfully");

Một số method khác của zlib

MethodMô tả
constants Returns an object containing Zlib constants
createDeflate() Creates a Deflate object
createDeflateRaw() Creates a DeflateRaw object
createGunzip() Creates a Gunzip object
createGzip() Creates a Gzip object
createInflate() Creates a Inflate object
createInflateRaw() Creates a InflateRaw object
createUnzip() Creates a Unzip object
xì hơi ()Nén một chuỗi hoặc bộ đệm, sử dụng độ lệch
xì hơiNén một chuỗi hoặc bộ đệm, đồng bộ, sử dụng độ lệch
Deflateraw ()Nén một chuỗi hoặc bộ đệm, sử dụng deflateraw
Deflaterawsync ()Nén một chuỗi hoặc bộ đệm, đồng bộ, sử dụng deflateraw
Gunzip ()Nén một chuỗi hoặc bộ đệm, sử dụng gunzip
gunzipsync ()Nén một chuỗi hoặc bộ đệm, đồng bộ, sử dụng gunzip
gzip ()Nén một chuỗi hoặc bộ đệm, sử dụng gzip
gzipsync ()Nén một chuỗi hoặc bộ đệm, đồng bộ, sử dụng gzip
thổi phồng lên()Giải nén một chuỗi hoặc bộ đệm, sử dụng bơm hơi
Ổn định ()Giải nén một chuỗi hoặc bộ đệm, đồng bộ, sử dụng tăng cường
Inflateraw ()Giải nén một chuỗi hoặc bộ đệm, sử dụng inflateraw
Inflaterawsync ()Giải nén một chuỗi hoặc bộ đệm, đồng bộ, sử dụng inflateraw
Giải nén ()Giải nén một chuỗi hoặc bộ đệm, sử dụng unzip
unzipsync ()Giải nén một chuỗi hoặc bộ đệm, đồng bộ, sử dụng unzip

Được rồi, xong!

Tải xuống mã ví dụ trên tại đNy.

—————————

References: