Skip to content

itgalaxy/rewrite-link-middleware

Repository files navigation

rewrite-link-middleware

NPM version Travis Build Status dependencies Status devDependencies Status Greenkeeper badge

Returns Express middleware that serves a service worker that resets any previously set service worker configuration. Useful for development.

Example you development site on 127.0.0.1:3000 and have html:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <a href="http://example.com/my/url">Link</a>
  </body>
</html>

Using this middleware you will get:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <a href="//127.0.0.1:3000/my/url">Link</a>
  </body>
</html>

Installation

npm i -D rewrite-link-middleware

API

const createRewriteLinkMiddleware = require("rewrite-link-middleware");

createRewriteLinkMiddleware(options)

Returns Express middleware that rewrite links in response.

Options

originalURL

Original URL hat you use in html document.

createRewriteLinkMiddleware({ originalURL: "http://example.com" });
createRewriteLinkMiddleware({
  originalURL: ["http://example.com", "http://another-example.com"]
});
Other

See other options

Examples

Simple app:

const express = require("express");
const createRewriteLinkMiddleware = require("rewrite-link-middleware");

const app = express();

app.use(createRewriteLinkMiddleware({ originalURL: "http://example.com" }));

app.get("/", function(req, res) {
  res.sendFile("index.html");
});

webpack-dev-server:

const createRewriteLinkMiddleware = require("rewrite-link-middleware");

module.exports = {
  // ...
  devServer: {
    before(app, server) {
      app.use(
        createRewriteLinkMiddleware({ originalURL: "http://example.com" })
      );
    }
    // ...
  }
  // ...
};