Compression

Compression can greatly decrease the size of the response body and hence increase the speed of a web app. Use the compression middleware to enable gzip compression.

Installation

Firstly, install the required package:

  1. $ npm i --save compression

Once the installation is completed, apply it as a global middleware.

  1. import * as compression from 'compression';
  2. // somewhere in your initialization file
  3. app.use(compression());

info Hint If you work with FastifyAdapter, consider using fastify-compress instead.

For a high-traffic website in production, the best way to put compression in place is to implement it at a reverse proxy level. In that case, you do not need to use compression middleware.