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
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 node index.js0 node index.js1 node index.js2 node index.js3 node index.js4 npm i express --save9 node index.js6 node index.js7 npm i express --save1 node index.js1 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: 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.jsmkdir Project cd Project npm init -y0 mkdir Project cd Project npm init -y1 npm i express --save1
npm i express --save0 npm i express --save1 npm i express --save2 mkdir Project cd Project npm init -y7 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 node index.js0 node index.js1 node index.js2 node index.js3 node index.js4 npm i express --save9 node index.js6 node index.js7 npm i express --save1 node index.js1 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) Để 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 var zlib = require('zlib'); Nén file (zip)Dưới đây là code ví dụ nén file node index.js6 thành file node index.js7 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.js8 và node index.js9 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.js7 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
Được rồi, xong! Tải xuống mã ví dụ trên tại đNy. ————————— References: |