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. Show
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.jsnpm 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 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: 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.jsnpm 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 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 --save21Mở 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 --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 filenpm i express --save26 thành file npm i express --save27 npm i express --save4Đầ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
Được rồi, xong! Tải xuống mã ví dụ trên tại đNy. ————————— References: |