Hướng dẫn gzip nodejs example - ví dụ về gzip nodejs

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%. 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: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: 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. Create a file on your project’s root directory and name index.js and write the following code. 

index.js

npm i express --save
0

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
npm i express --save
20
npm i express --save
21
npm i express --save
22
npm i express --save
23
npm i express --save
24
npm i express --save
9
npm i express --save
26
npm i express --save
27
npm i express --save
1
npm i express --save
21

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

npm i express --save
2

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:http://localhost:8080/, Without compression, the response returned by the server will weigh around 16 kB, as shown below:

Hướng dẫn gzip nodejs example - ví dụ về gzip nodejs

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.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:Create an empty NodeJS application with the following command:

npm i express --save
3

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

npm i express --save
4

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. Create a file on your project’s root directory and name index.js and write the following code. 

index.js

npm i express --save
30
npm i express --save
31
npm i express --save
1

npm i express --save
0

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
npm i express --save
20
npm i express --save
21
npm i express --save
22
npm i express --save
23
npm i express --save
24
npm i express --save
9
npm i express --save
26
npm i express --save
27
npm i express --save
1
npm i express --save
21

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

npm i express --save
2

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: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:


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 2: Cài đặt mô -đun ExpressJS bằng lệnh sau: Install the compression and ExpressJS module using the following command:

npm i express --save0npm i express --save1npm i express --save2npm i express --save37npm i express --save3npm i express --save4npm i express --save5npm i express --save6npm i express --save7npm i express --save8npm i express --save9npm i express --save20npm i express --save21npm i express --save22npm i express --save23 npm i express --save24npm i express --save9npm i express --save26npm i express --save27npm i express --save1npm i express --save21

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:

npm i express --save
25

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

npm i express --save
1

(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

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

Nén file (zip)

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

npm i express --save
26 thành file
npm i express --save
27
npm i express --save
4

Đầu vào của method 

npm i express --save28 và npm i express --save29 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
npm i express --save
27 thành file
npm i express --save
11.
npm i express --save
9
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ơi Né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
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: