Compressing HTTP responses
compression is a middleware function that compresses the response body that will be send to the client. This module uses the zlib
module https://nodejs.org/api/zlib.html that supports the following content-encoding mechanisms:
- gzip
- deflate
The Accept-Encoding
HTTP header is used to determine which content-encoding mechanism is supported on the client-side (for example web browser) while the Content-Encoding
HTTP header is used to tell the client which content encoding mechanism was applied to the response body.
compression
is a configurable middleware function. It accepts an options
object as the first argument to define a specific behavior for the middleware and also to pass zlib
options as well.
Getting ready
In this recipe, we will see how to configure and use the compression
NPM module to compress the request body sent to the client. Before you start, create a new package.json
file with the following content:
{ "dependencies": { "compression": "1.7.2...