Exciting release!Introducing "safe npm". Learn more
Socket
Log inDemoInstall

string-replace-middleware

Package Overview
Dependencies
2
Maintainers
1
Versions
4
Issues
File Explorer

Advanced tools

string-replace-middleware

Express middleware to replace strings in response stream on the fly

    1.0.2latest
    GitHub

Version published
Maintainers
1
Weekly downloads
732
decreased by-26.51%

Weekly downloads

Readme

Source

String Replace Middleware

A middleware for Express that allows for stream-based string replacement before sending responses.

Installation

Install in your Express project using:

npm install --save string-replace-middleware

Usage

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);

Configuration

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

Last updated on 13 Aug 2020

Did you know?

Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install Socket
Socket
support@socket.devSocket SOC 2 Logo

Product

  • Package Issues
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc