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 --save0
npm i express --save0
npm i express --save1
npm i express --save2
npm i express --save3
npm i express --save4
npm i express --save5
npm i express --save6
npm i express --save7
npm i express --save8
npm i express --save9
npm i express --save20
npm i express --save21
npm i express --save22
npm i express --save23
npm i express --save24
npm i express --save9
npm i express --save26
npm i express --save27
npm i express --save1
npm i express --save21
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 --save2
Mở trình duyệt và truy cập URL // 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://localhost:8080/, Without compression, the response returned by the server will weigh around 16 kB, as shown below://localhost:8080/, Without compression, the response returned by the server will weigh around 16 kB, 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.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 --save3
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 --save4
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 --save30
npm i express --save31
npm i express --save1
npm i express --save0
npm i express --save0
npm i express --save1
npm i express --save2
npm i express --save3
npm i express --save4
npm i express --save5
npm i express --save6
npm i express --save7
npm i express --save8
npm i express --save9
npm i express --save20
npm i express --save21
npm i express --save22
npm i express --save23
npm i express --save24
npm i express --save9
npm i express --save26
npm i express --save27
npm i express --save1
npm i express --save21
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 --save2
Mở trình duyệt và truy cập URL // 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://localhost:8080/, Without compression, the response returned by the server will weigh around 16 kB, as shown below://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 --save
0npm i express --save
1npm i express --save
2npm i express --save
37npm i express --save
3npm i express --save
4npm i express --save
5npm i express --save
6npm i express --save
7npm i express --save
8npm i express --save
9npm i express --save
20npm i express --save
21npm i express --save
22npm i express --save
23 npm i express --save
24npm i express --save
9npm i express --save
26npm i express --save
27npm i express --save
1npm i express --save
21
Mở trình duyệt và truy cập URL // 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://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 --save25
Code ví dụ Node.js nén, giải nén file – module Zlib
npm i express --save1
[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 modulezlib 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 --save26 thành file
npm i express --save27
npm i express --save4Đầu vào của method
npm i express --save
28 và npm i express --save
29 sẽ là url của file được dùng để nén và url của file kết quả sau khi được nén
npm i express --save27 thành file npm i express --save11. npm i express --save9 | Một số method khác của zlib |
Method | Mô 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: