Git Product home page Git Product logo

string-replace-middleware's Introduction

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.

string-replace-middleware's People

Contributors

bfncs avatar carlo-quinonez avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

carlo-quinonez

string-replace-middleware's Issues

Binary content shouldn't be replace

This middleware break all binary content.

If your express serve binary files, string-replace-middleware will replace the binary content by an utf8 encoded content.

Solutions

  • Check the res content-type before applying (maybe add an option for that)
  • Decode the binary , replace, reencode as binary ?

Workaround

Can be avoided by loading the middleware only on specifics paths.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.