Registry / http-networking / string-replace-middleware

string-replace-middleware

JSON →
library1.1.0jsnpmunverified

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.

npm install string-replace-middleware
INSTALL
IMPORT
SIG · STRING-REPLACE-MID
S
string-replace-middleware
http-networkingjavascriptv1.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

stringReplace
import { stringReplace } from 'string-replace-middleware'
import stringReplace from 'string-replace-middleware'
The library exports a named export, not a default export. ESM import syntax shown; CommonJS require works the same way.
stringReplace
const { stringReplace } = require('string-replace-middleware')
const stringReplace = require('string-replace-middleware')
CommonJS require must use destructuring to obtain the named export. Direct require returns the module object.
stringReplace
const stringReplace = require('string-replace-middleware').stringReplace
const stringReplace = require('string-replace-middleware')
Alternative CommonJS pattern: assign the named export explicitly to a variable.

Sets up an Express app with string replacement middleware that replaces 'foo' with 'bar' and 'hello' with 'world' in all responses matching default content types.

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'));
Debug
Known issues
gotchaMiddleware 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.
affects: all
gotchaThe 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.
affects: all
gotchaReplacement 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).
affects: all
Errors
Common errors & fixes
TypeError: stringReplace is not a function
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')`.
Cannot find module 'string-replace-middleware'
Package not installed or Node.js version < 10 (engines requirement).
fix
Run `npm install string-replace-middleware` and ensure Node.js >= 10.
TypeError: Cannot read property 'foo' of undefined
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.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies
expressrequiredPeer dependency; designed as Express middleware and requires the Express request/response object
Agent activity
13 hits · last 30 days
node
10
Amazon
1
OpenAI (training)
1
Resources
string-replace-middleware — npm install string-replace-middleware · libregistry