node.js入门
Compression in Node.js and Express decreases the downloadable amount of data that’s served to users. Through the use of this compression, we can improve the performance of our Node.js applications as our payload size is reduced drastically.
Node.js和Express中的压缩可减少提供给用户的可下载数据量。 通过使用这种压缩,我们可以大大减少有效负载大小,从而提高Node.js应用程序的性能。
There are two methods of compression. One is calling it directly in your Node.js app using the compression
middleware, and the other is to use it at a reverse proxy level through software like NGINX.
有两种压缩方法。 一种是使用compression
中间件在Node.js应用程序中直接调用它,另一种是通过类似NGINX的软件在反向代理级别使用它。
如何设置压缩 (How To Set Up Compression)
To start using compression in your Node.js application, you can use the compression
middleware in the main file of your Node.js app. This will enable GZIP, which supports different compression schemes. This will make your JSON response and other static file responses smaller.
要开始在Node.js应用程序中使用压缩,可以在Node.js应用程序的主文件中使用compression
中间件。 这将启用GZIP,它支持不同的压缩方案。 这将使您的JSON响应和其他静态文件响应变小。
First, you’ll need to install the npm package for compression
:
首先,您需要安装npm软件包进行compression
:
$ npm i compression --save
Then you can use the module in your application after you initialize your server, like with Express.js:
然后,您可以在初始化服务器后在应用程序中使用模块,例如Express.js:
const compression = require('compression');
const express = require('express');
const app = express();
// compress all responses
app.use(compression());
app.get('/', (req, res) => {
const animal = 'alligator';
// Send a text/html file back with the word 'alligator' repeated 1000 times
res.send(animal.repeat(1000));
});
// ...
In the example above, we call a GET operation that will send back a text/html
file with the word alligator
printed 1000 times. Without compression, the response would come back with a size of around 9kb.
在上面的示例中,我们调用GET操作,该操作将发送回text/html
文件,其中的alligator
印有1000次。 如果不进行压缩,则响应将以大约9kb的大小返回。
If you turn on compression, the response is sent with a header that states Content-Encoding: gzip
, and instead is only 342B.
如果打开压缩,则发送的响应带有标有Content-Encoding: gzip
的标头,而仅是342B。
压缩选项 (Options For Compression)
In addition to the default setting, you can customize your compression to fit your use case. There are several different properties that you can use in the options object. To get a full list of properties that you can choose, check out the compression
documentation.
除了默认设置,您还可以自定义压缩以适合您的用例。 您可以在options对象中使用几个不同的属性。 要获取您可以选择的完整属性列表,请查看compression
文档 。
To add the options for your compression
, your code will look a little something like this:
要添加compression
选项,您的代码将看起来像这样:
const shouldCompress = (req, res) => {
if (req.headers['x-no-compression']) {
// don't compress responses if this request header is present
return false;
}
// fallback to standard compression
return compression.filter(req, res);
};
app.use(compression({
// filter decides if the response should be compressed or not,
// based on the `shouldCompress` function above
filter: shouldCompress,
// threshold is the byte threshold for the response body size
// before compression is considered, the default is 1kb
threshold: 0
}));
And there you have it! Make sure you use compression
for your Node.js app to keep your payload sizes small and snappy!
在那里,您拥有了! 确保对Node.js应用程序使用compression
,以使有效负载尺寸小而生动!
翻译自: https://www.digitalocean.com/community/tutorials/nodejs-compression
node.js入门