Skip to content

How to rewrite URLs to local or remote destinations

Lloyd Brookes edited this page Jun 9, 2019 · 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>'

For example.

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

Re-route any stylesheet under /css/ to /build/css/. Routes are specified using path-to-regexp v3 routing syntax. This syntax is virtually identical to Express v4 routing syntax except for wildcard expressions, which should be written as (.*) instead of *.

$ 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.). In the replace expression, use $1, $2 etc to replace each matched group.

$ 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