The `modify-response-middleware` package provides an Express middleware designed to intercept and transform HTTP response bodies before they are sent to the client. It transparently handles various compression encodings like Gzip, Brotli, Deflate, and Br, allowing developers to modify response data regardless of its original compression status. As of version 1.1.0, it supports both uncompressed and compressed payloads, offering options to disable caching (`noCache`). This middleware is particularly useful for injecting data, sanitizing output, or performing last-minute transformations on API responses. Its release cadence appears stable but infrequent, typical for a focused utility. A key differentiator is its built-in handling of common compression algorithms, abstracting away the complexities of decompression and re-compression during modification.
npm install modify-response-middlewareVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to integrate `modify-response-middleware` into an Express application to intercept and modify JSON response bodies before they are sent to the client.
Ensure `app.use(modifyRes(...))` is declared early in your Express application's middleware stack, ideally before route definitions. If using router-level middleware, place it before route-specific handlers within that router.
Profile your application with and without the middleware in production scenarios. Consider applying the middleware only to specific routes (using `app.use('/path', modifyRes(...))`) or conditionally based on response size if performance becomes an issue.If `noCache` is not suitable, ensure your modified response handler explicitly removes or recalculates `Content-Length` and `ETag` headers if the body size changes significantly. Monitor network responses to ensure headers align with modified content.
Always validate the content type (e.g., `res.get('Content-Type')`) within your `modifyRes` callback before attempting type-specific parsing (e.g., `JSON.parse()`). Wrap parsing logic in `try...catch` blocks to handle malformed or unexpected data gracefully. Return the original `content` buffer if parsing/modification fails.Before parsing, check `res.get('Content-Type')` within your `modifyRes` callback to ensure it's `application/json`. Add a `try...catch` block around `JSON.parse()` to handle non-JSON or malformed responses gracefully.Always add a check `if (content && Buffer.isBuffer(content))` before calling `content.toString()` or attempting to process `content`. For empty responses, `content` might legitimately be `null` or an empty buffer, in which case it should be returned as is.
When the response body is modified, the middleware should ideally reset or remove the `Content-Length` header. If issues persist, ensure the middleware or your callback removes `res.removeHeader('Content-Length')` or explicitly sets `res.setHeader('Content-Length', Buffer.byteLength(newContent))` for non-compressed content.