Socket
Socket
Sign inDemoInstall

compression

Package Overview
Dependencies
3
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    compression

Compression middleware for connect and node.js


Version published
Weekly downloads
18M
decreased by-1.3%
Maintainers
1
Created
Weekly downloads
 

Package description

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

Readme

Source

compression Build Status

Connect's compress middleware as its own module. Works with and without Connect.

var compress = require('compression')()

http.createServer(function (req, res) {
  compress(req, res, function (err) {
    if (err) throw err

    res.end('hello world')
  })
})

var app = require('connect')()
app.use(compress)

API

var middleware = compress([options])

In addition to zlib options, additional options are:

  • threshold <1kb> - only compress the response if the byte size is at or above a threshold
  • filter - a filter callback function

License

The MIT License (MIT)

Copyright (c) 2014 Jonathan Ong me@jongleberry.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

FAQs

Last updated on 01 Jan 2014

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc