Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

compression

Package Overview
Dependencies
Maintainers
7
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

compression

Compression middleware for connect and node.js

  • 1.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
18M
decreased by-6.65%
Maintainers
7
Weekly downloads
 
Created

What is compression?

The compression npm package is a middleware for Node.js that enables response compression, typically used with Express.js. It can compress response bodies for requests that traverse through the middleware, thus reducing the size of the response sent to clients and improving web application performance.

What are compression's main functionalities?

Compress response bodies

This code demonstrates how to use the compression middleware in an Express.js application to compress response bodies for all routes.

const express = require('express');
const compression = require('compression');

const app = express();

// Use compression middleware
app.use(compression());

// Example route
app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.listen(3000);

Filter requests to compress

This code shows how to use a custom filter function to decide whether to compress responses based on the request headers.

const express = require('express');
const compression = require('compression');

const app = express();

// Compression options with filter
const shouldCompress = (req, res) => {
  if (req.headers['x-no-compression']) {
    // Don't compress responses if this request header is present
    return false;
  }

  // Fallback to the standard compression filter function
  return compression.filter(req, res);
};

app.use(compression({ filter: shouldCompress }));

app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.listen(3000);

Other packages similar to compression

FAQs

Package last updated on 16 Oct 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc