Skip to content

How to rewrite URLs to local or remote destinations

Lloyd Brookes edited this page Jul 17, 2017 · 17 revisions

Local targets

Your application requested /css/style.css but you'd like to serve /css/new-theme.css. The syntax is:

$ ws --rewrite '<source route> -> <destination route>'

Routes are specified using Express routing syntax. For example.

$ ws --rewrite '/css/style.css -> /css/new-theme.css'

Re-route any stylesheet under /css/ to /build/css/.

$ ws --rewrite '/css/:stylesheet -> /build/css/:stylesheet'

Re-route the entire directory structure under /css/ to /build/css/ (this rewrites /css/a as /build/css/a, /css/a/b/c as /build/css/a/b/c etc.)

$ ws --rewrite '/css/* -> /build/css/$1'

Proxied requests

If the to URL contains a remote host, local-web-server will act as a proxy - fetching and responding with the remote resource.

Mount the npm registry locally:

$ ws --rewrite '/npm/* -> http://registry.npmjs.org/$1'

Map local requests for repo data to the Github API:

$ ws --rewrite '/:user/repos/:name -> https://api.github.com/repos/:user/:name'

Config

Rewrite rules are stored in config as an array of { from: string, to: string } objects.

module.exports = {
  rewrite: [
    { from: "/css/*", "to": "/build/styles/$1" },
    { from: "/npm/*", "to": "http://registry.npmjs.org/$1" },
    { from: "/broken/*", "to": "http://localhost:9999" },
    { from: "/:user/repos/:name", "to": "https://api.github.com/repos/:user/:name" }
  ]
}
Clone this wiki locally