node.js入门_Node.js中的压缩入门

news/2024/7/16 5:05:51

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入门


http://www.niftyadmin.cn/n/3649775.html

相关文章

跨域或者Internet访问Remoting[Remoting FAQ]

[Remoting FAQ]跨域或者Internet访问RemotingVersionDateCreatorDescription1.0.0.12006-6-1郑昀Ultrapower草稿继续阅读之前,我们假设您熟悉以下知识:n Remoting[需求]虽然说,Remoting一般都在同一个域内调用,但有时候&a…

Android自定义广播接收

Android自定义广播接收 实现效果: MainActivity.java代码: package com.henu.broadcastsample;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; im…

flutter顶部小部件_使用VoidCallback和Function(x)与Flutter进行小部件通信

flutter顶部小部件In this article we’re going to investigate how we can use callback-style events to communicate between widgets with Flutter. 在本文中,我们将研究如何使用Flutter在回调控件之间使用回调风格的事件进行通信。 Why is this important? …

[MSN谈话]专注、口碑和猎头

[网友]: 我原先做.NET WAP的,你的工作是做哪方面的我: 我什么都作。[网友]: 我觉得我做的算多的了,一看才知道,小巫见大巫了我: 移动增值领域我基本都涉及过。[网友]: 我从网络安全到对日外包,再到asp php网站开发之后.NET wap开发…

Python编写数字转换成中文大写

问题描述:输入一串数字金额,然后打印出它的中文大写。 利用列表下标进行转换。 list1 [零, 壹, 贰, 叁, 肆, 伍, 陆, 柒, 捌, 玖, 拾] list2 [圆, 拾, 佰, 仟, 萬]money str(int(input("请输入金额:"))) # 预防输入0开头的数字 money2 …

[推介]明朝的那些事儿-历史应该可以写得好看

无意连接到了一篇把历史讲述得格外精彩的贴子,号称天涯史上最强贴之一:http://cache.tianya.cn/publicforum/Content/no05/1/34523.shtml ,《明朝的那些事儿-历史应该可以写得好看》,写得还真不错,引人入胜…

Python编写2-1000的回文素数

问题描述:找出2-1000的回文素数。 import math for i in range(2, 1001):for j in range(2, int(math.sqrt(i))):if i % j 0:breakelse:m str(i)if m[0] m[-1]:print("%d为回文素数" % i) Python中for循环和else是可以连用的,当for循环执…

go 创建自定义包_在Go中创建自定义错误

go 创建自定义包介绍 (Introduction) Go provides two methods to create errors in the standard library, errors.New and fmt.Errorf. When communicating more complicated error information to your users, or to your future self when debugging, sometimes these two …