Home
Docs
GitHub
Pricing
Blog
Log In

Run Sandworm Audit for your App

Get started
Generated on Oct 9, 2023 via pnpm

compression 1.4.4

Node.js compression middleware
Package summary
Share
0
issues
0
licenses
Package created
1 Jan 2014
Version published
12 May 2015
Maintainers
1
Total deps
0
Direct deps
0
License
MIT

Issues

0
This package has no issues

Frequently Asked Questions

What does compression do?

The "compression" is a Node.js middleware that aims to compress response bodies for all requests that go through the middleware. It supports two compression codings: "deflate" and "gzip". This is handy for efficient data handling in your Node.js web application, resulting in decreased load times and improved performance due to less data being transferred over the network.

How do you use compression?

To install the "compression" middleware, simply use the npm install command in your terminal like so:

$ npm install compression

Then, you require the compression middleware and use it in your application as illustrated in the following example:

var compression = require('compression')
var express = require('express')

var app = express()

// compress all responses
app.use(compression())

// add all routes

This example demonstrates compression being used with the Express.js library. By using the middleware via the app.use(compression()) call, all responses of your Express.js application will be compressed.

For more advanced usage, you can customise the behaviour by passing an options object to the compression function as demonstrated:

var compression = require('compression')
var express = require('express')

var app = express()
app.use(compression({ filter: shouldCompress }))

function shouldCompress (req, res) {
  if (req.headers['x-no-compression']) {
    // don't compress responses with this request header
    return false
  }

  // fallback to standard filter function
  return compression.filter(req, res)
}

In this example, the filter option is used to determine whether the response should be compressed or not.

Where are the compression docs?

For comprehensive information about the compression middleware, the API, and various usage scenarios, it is best to refer to the official GitHub repository at git+https://github.com/expressjs/compression.git. There is no separate documentation site, but the readme file in the GitHub repo provides extensive information, including more detailed code examples and a full rundown of all the possible configuration options and their usage. The module can also be found on npm at https://npmjs.org/package/compression.