|
1 | 1 | ### Compression
|
2 | 2 |
|
3 |
| -Compression can greatly decrease the size of the response body, thereby increasing the speed of a web app. Use the [compression](https://github.com/expressjs/compression) middleware package to enable gzip compression. |
| 3 | +Compression can greatly decrease the size of the response body, thereby increasing the speed of a web app. |
4 | 4 |
|
5 |
| -#### Installation |
| 5 | +For **high-traffic** websites in production, it is strongly recommended to offload compression from the application server - typically in a reverse proxy (e.g., Nginx). In that case, you should not use compression middleware. |
| 6 | + |
| 7 | +#### Use with Express (default) |
| 8 | + |
| 9 | +Use the [compression](https://github.com/expressjs/compression) middleware package to enable gzip compression. |
6 | 10 |
|
7 | 11 | First install the required package:
|
8 | 12 |
|
9 | 13 | ```bash
|
10 | 14 | $ npm i --save compression
|
11 | 15 | ```
|
12 | 16 |
|
13 |
| -Once the installation is complete, apply the compression middleware as global middleware. |
| 17 | +Once the installation is complete, apply the compression middleware as a global middleware. |
14 | 18 |
|
15 | 19 | ```typescript
|
16 | 20 | import * as compression from 'compression';
|
17 | 21 | // somewhere in your initialization file
|
18 | 22 | app.use(compression());
|
19 | 23 | ```
|
20 | 24 |
|
21 |
| -> info **Hint** If using the `FastifyAdapter`, consider using [fastify-compress](https://github.com/fastify/fastify-compress) instead. |
| 25 | +#### Use with Fastify |
22 | 26 |
|
23 |
| -For **high-traffic** websites in production, it is strongly recommended to offload compression from the application server - typically in a reverse proxy (e.g., Nginx). In that case, you should not use compression middleware. |
| 27 | +If using the `FastifyAdapter`, you'll want to use [fastify-compress](https://github.com/fastify/fastify-compress): |
| 28 | + |
| 29 | +```bash |
| 30 | +$ npm i --save fastify-compress |
| 31 | +``` |
| 32 | + |
| 33 | +Once the installation is complete, apply the fastify-compress middleware as a global middleware. |
| 34 | + |
| 35 | +```typescript |
| 36 | +import * as compression from 'fastify-compress'; |
| 37 | +// somewhere in your initialization file |
| 38 | +app.register(compression); |
| 39 | +``` |
0 commit comments