
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
string-replace-middleware
Advanced tools
Express middleware to replace strings in response stream on the fly
A middleware for Express that allows for stream-based string replacement before sending responses.
Install in your Express project using:
npm install --save string-replace-middleware
After installing, you can add it as a middleware to your project and hand over a map of replacements. To replace every occurence of foo
with bar
.
const express = require('express');
const { stringReplace } = require('string-replace-middleware');
const app = express();
app.use(stringReplace({
'foo': 'bar',
}));
app.listen(3000);
Use it to serve static files with replacements like in this example:
const express = require('express');
const serveStatic = require('serve-static');
const { stringReplace } = require('string-replace-middleware');
const app = express();
app.use(stringReplace({
'foo': 'bar',
}));
app.use(serveStatic('public'));
app.listen(3000);
The Content-Type header of responses is checked against a regex before modification. The regex is configurable by passing in an options object like this:
const options = {
contentTypeFilterRegexp: /^text\/|^application\/json$|^application\/xml$/,
}
app.use(stringReplace({
'foo': 'bar',
}, options));
The default regex is /^text\/|^application\/json$|^application\/xml$/
, which will match text/*
, application/json
, and application/xml
. Any response with a Content-Type header that doesn't match the regex is ignored and passed-through without modification.
Also any response without a Content-Type header is ignored and passed-through without any modification.
FAQs
Express middleware to replace strings in response stream on the fly
We found that string-replace-middleware demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.