Socket
Socket
Sign inDemoInstall

compression

Package Overview
Dependencies
6
Maintainers
7
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
Maintainers
7
Install size
451 kB
Created

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

NPM Version NPM Downloads Build Status Test Coverage Gratipay

Node.js compression middleware.

Install

$ npm install compression

API

var compression = require('compression')

compression(options)

Returns the compression middleware using the given options.

app.use(compression({
  threshold: 512
}))
Options
  • threshold <1kb> - response is only compressed if the byte size is at or above this threshold.
  • filter - a filtering callback function. Uses Compressible by default.

In addition to these, zlib options may be passed in to the options object.

res.flush

This module adds a res.flush() method to force the partially-compressed response to be flushed to the client.

Examples

express/connect

When using this module with express or connect, simply app.use the module as high as you like. Requests that pass through the middleware will be compressed.

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

var app = express()

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

// add alll routes

Server-Sent Events

Because of the nature of compression this module does not work out of the box with server-sent events. To compress content, a window of the output needs to be buffered up in order to get good compression. Typically when using server-sent events, there are certain block of data that need to reach the client.

You can achieve this by calling res.flush() when you need the data written to actually make it to the client.

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

var app = express()

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

// server-sent event stream
app.get('/events', function (req, res) {
  res.setHeader('Content-Type', 'text/event-stream')
  res.setHeader('Cache-Control', 'no-cache')

  // send a ping approx every 2 seconds
  var timer = setInterval(function () {
    res.write('data: ping\n\n')

    // !!! this is the important part
    res.flush()
  }, 2000)

  res.on('close', function () {
    clearInterval(timer)
  })
})

License

MIT

FAQs

Last updated on 10 Dec 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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc