{"library":"modify-response-middleware","title":"Express Response Modification Middleware","description":"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.","language":"javascript","status":"active","last_verified":"Thu Apr 23","install":{"commands":["npm install modify-response-middleware"],"cli":null},"imports":["import modifyRes from 'modify-response-middleware'","import { Buffer } from 'node:buffer'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import express from 'express';\nimport modifyRes from 'modify-response-middleware';\n\nconst app = express();\n\napp.use(express.json()); // Ensure JSON body parsing for incoming requests if needed\n\n// Apply the modification middleware early in the stack\napp.use(modifyRes((content, req, res) => {\n  if (content && res.get('Content-Type')?.includes('application/json')) {\n    try {\n      const data = JSON.parse(content.toString());\n      // Example: Modify the age property of the JSON response\n      data.age = (data.age || 0) + 2;\n      data.message = 'Response modified by middleware!';\n      return Buffer.from(JSON.stringify(data));\n    } catch (e) {\n      console.error('Failed to parse or modify JSON content:', e);\n      // Return original content if modification fails\n      return content;\n    }\n  }\n  // Always return content, even if no modification occurred or type didn't match\n  return content;\n}, {\n  noCache: true, // Prevents 304 Not Modified responses, useful for dynamic content\n}));\n\napp.get('/user', (req, res) => {\n  res.json({\n    name: 'Tom',\n    age: 18,\n    status: 'active'\n  });\n});\n\napp.get('/html', (req, res) => {\n  res.send('<h1>Hello, World!</h1><p>This is an HTML response.</p>');\n});\n\nconst PORT = 3000;\napp.listen(PORT, () => {\n  console.log(`Server listening on port ${PORT}`);\n  console.log(`Test with: curl http://localhost:${PORT}/user`);\n  console.log(`Test with: curl http://localhost:${PORT}/html`);\n});","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}