string-replace-middleware
raw JSON → 1.1.0 verified Sat Apr 25 auth: no javascript
Express middleware that performs stream-based string replacement in HTTP response bodies on the fly. Current stable version is 1.1.0, released under MIT license, with infrequent updates. Key differentiator: works on the response stream before sending, unlike typical string replacement that buffers entire responses. Supports configurable Content-Type filtering via regex (default: text/*, application/json, application/xml). Ships TypeScript type declarations. Requires Node >=10 and Express. Not recommended for production with large or streaming responses due to potential performance overhead.
Common errors
error TypeError: stringReplace is not a function ↓
cause Incorrect import style — using default import instead of named import.
fix
Use
const { stringReplace } = require('string-replace-middleware') instead of const stringReplace = require('string-replace-middleware'). error Cannot find module 'string-replace-middleware' ↓
cause Package not installed or Node.js version < 10 (engines requirement).
fix
Run
npm install string-replace-middleware and ensure Node.js >= 10. error TypeError: Cannot read property 'foo' of undefined ↓
cause If the replacement map contains a key that is a string with special regex characters (e.g., '.'), the library may throw due to improper regex escaping (note: actual library behavior — it uses plain string replace, not regex, so this error unlikely but possible if misuse). Alternatively, this error can occur if the middleware is not applied correctly.
fix
Ensure you call
app.use(stringReplace({...})) correctly. The library uses string replacement, not regex, so most special characters are safe. Check for proper Express setup. Warnings
gotcha Middleware only processes responses with Content-Type matching the regex. Responses without Content-Type or with non-matching types (e.g., image/png) are passed through unmodified. ↓
fix Verify that your responses set an appropriate Content-Type header. Use the options.contentTypeFilterRegexp to customize matching.
gotcha The middleware buffers the entire response body to perform replacement, which can cause high memory usage and increased latency for large responses or high-traffic endpoints. ↓
fix Avoid using this middleware for large file downloads or streaming endpoints. Consider alternative approaches like pre-processing strings before sending.
gotcha Replacement is applied to the raw response stream, including HTML tags and JavaScript code. Replacements may inadvertently break content syntax (e.g., replacing 'var' in JavaScript). ↓
fix Use targeted replacements that are unlikely to appear in code or structure. Consider adding logic to avoid replacing inside certain contexts (not provided by the library).
Install
npm install string-replace-middleware yarn add string-replace-middleware pnpm add string-replace-middleware Imports
- stringReplace wrong
import stringReplace from 'string-replace-middleware'correctimport { stringReplace } from 'string-replace-middleware' - stringReplace wrong
const stringReplace = require('string-replace-middleware')correctconst { stringReplace } = require('string-replace-middleware') - stringReplace wrong
const stringReplace = require('string-replace-middleware')correctconst stringReplace = require('string-replace-middleware').stringReplace
Quickstart
import express from 'express';
import { stringReplace } from 'string-replace-middleware';
const app = express();
app.use(stringReplace({ 'foo': 'bar', 'hello': 'world' }));
app.get('/', (req, res) => {
res.send('foo says hello');
});
app.listen(3000, () => console.log('Listening on 3000'));