{"id":17928,"library":"resp-modifier","title":"Connect/Express Response Modifier Middleware","description":"resp-modifier is a Node.js middleware designed for Connect and Express applications, enabling the modification of HTTP response bodies, particularly HTML. It generalizes the functionality found in packages like `connect-livereload`, focusing solely on response modification rather than live reload specifics. The package allows developers to define multiple replacement rules to transform the outgoing response content, making it suitable for tasks such as injecting scripts, modifying links, or censoring content dynamically. The current stable version is 6.0.2. While the release cadence is not explicitly defined, regular updates suggest active maintenance. Key differentiators include its focused scope on response manipulation and compatibility with standard Connect-style middleware architectures.","status":"active","version":"6.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/shakyshane/resp-modifier","tags":["javascript"],"install":[{"cmd":"npm install resp-modifier","lang":"bash","label":"npm"},{"cmd":"yarn add resp-modifier","lang":"bash","label":"yarn"},{"cmd":"pnpm add resp-modifier","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For modern Node.js projects using ES Modules. The `require()` syntax is for CommonJS environments, historically common given Node.js versions supported by this package.","wrong":"const respModifier = require('resp-modifier');","symbol":"respModifier","correct":"import respModifier from 'resp-modifier';"},{"note":"CommonJS syntax, suitable for older Node.js projects or environments where `type: module` is not set in package.json.","symbol":"respModifierCJS","correct":"const respModifierCJS = require('resp-modifier');"}],"quickstart":{"code":"import express from 'express';\nimport respModifier from 'resp-modifier';\n\nconst app = express();\nconst PORT = process.env.PORT || 3000;\n\n// Basic usage: replace 'world' with 'developer' in HTML responses\napp.use(respModifier({\n  rules: [\n    {\n      match: /world/g,\n      replace: 'developer'\n    },\n    {\n      match: /<\\/body>/,\n      replace: '<script>console.log(\"Response modified!\")</script></body>'\n    }\n  ]\n}));\n\napp.get('/', (req, res) => {\n  res.status(200).send('<h1>Hello, world!</h1><p>This is a test page.</p>');\n});\n\napp.get('/json', (req, res) => {\n  // resp-modifier typically targets HTML responses, \n  // JSON responses might not be affected or could be corrupted if not handled carefully.\n  res.json({ message: 'Hello, JSON world!' });\n});\n\napp.listen(PORT, () => {\n  console.log(`Server listening on http://localhost:${PORT}`);\n  console.log('Try visiting / and /json');\n});","lang":"javascript","description":"Demonstrates setting up `resp-modifier` as Express middleware to perform string replacements and inject content into HTML responses."},"warnings":[{"fix":"Ensure `app.use(respModifier(...))` is called early in your middleware chain, before route handlers that `res.send()` or `res.json()`.","message":"Middleware order is critical. `resp-modifier` must be placed before any middleware that sends the final response or compresses it, to ensure it can intercept and modify the response body. If placed incorrectly, it may not modify content or could cause 'Headers already sent' errors.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Be mindful of performance implications for large response bodies. Consider adding checks within your modification logic to only modify responses of specific content types or sizes if performance is critical.","message":"When modifying the response body, especially in a streaming context, incorrect `Content-Length` headers can lead to truncated or incomplete responses from the client's perspective. `resp-modifier` typically buffers the response to perform modifications, which can impact performance for very large responses.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Utilize the middleware's configuration to specify content types to target or include logic to bypass modification for certain `res.getHeader('Content-Type')` values. For example, some libraries allow a `filter` function to control when modification applies.","message":"Modifying non-HTML content types (like JSON, images, or compressed data) without specific content-type handling can corrupt the response. `resp-modifier` is primarily designed for text-based modifications, particularly HTML.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Review the order of your Express middleware. Ensure `resp-modifier` is positioned before any route handlers that call `res.send()` or `res.json()`, and before other middleware that might finalize the response stream.","cause":"Middleware that attempts to modify headers or send a response after a preceding middleware or route handler has already sent the response.","error":"Error: Can't set headers after they are sent."},{"fix":"Verify that `resp-modifier` is correctly configured for your content's encoding (e.g., UTF-8). Check if there are any options to manage `Content-Length` header auto-correction. If modifications are drastic, consider debugging the middleware's internal buffer handling.","cause":"The `Content-Length` header is incorrect after modification, or the modification process itself is corrupting the buffer/stream. This is common if the middleware doesn't correctly update headers or handle different encodings.","error":"Client receives incomplete or malformed response body."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}