Node.js compression middleware for Express/Connect that provides gzip and deflate encoding. Current stable version is 1.8.1 (August 2024). Maintained by the Express.js team with monthly releases. Handles content negotiation, cache-control respecting, and configurable zlib options. Key differentiator is seamless integration with Express routing, supporting threshold, filter, and level settings. Not a standalone compressor; must be used with Node.js HTTP server or framework. Legacy support for Node >=0.8.0.
npm install compresionVerified import paths — ran on the pinned version, not inferred.
Creates an Express server with compression middleware applied globally to compress responses.
Update any code that explicitly uses Buffer to use safe-buffer's Buffer if needed.
Replace `res._header` with `res.headersSent` in custom filter or hook functions.
Ensure `compression()` is inserted in middleware stack before any route handlers that write to the response.
Remove `no-transform` from Cache-Control header if you want forced compression.
Explicitly set `level` option if you need a specific compression level.
Use `import compression from 'compression'` or use `const compression = require('compression').default` if using CommonJS in ESM context.Ensure `level` option is a number between -1 and 9.
Move `app.use(compression())` before all route handlers.