Stefan Steinhart 4008768f86 docs(@angular/cli): adds multiple entries how-to
If there is a need to define multiple entries or otherwise more complex configuration a js file can be used to configure the proxy. These changes document how to do so.
2017-06-15 13:23:28 -04:00

1.3 KiB

Proxy To Backend

Using the proxying support in webpack's dev server we can highjack certain urls and send them to a backend server. We do this by passing a file to --proxy-config

Say we have a server running on http://localhost:3000/api and we want all calls to http://localhost:4200/api to go to that server.

We create a file next to projects package.json called proxy.conf.json with the content

{
  "/api": {
    "target": "http://localhost:3000",
    "secure": false
  }
}

You can read more about what options are available here webpack-dev-server proxy settings

and then we edit the package.json file's start script to be

"start": "ng serve --proxy-config proxy.conf.json",

now run it with npm start

Multiple entries

If you need to proxy multiple entries to the same target define the configuration in proxy.conf.js e.g.

const PROXY_CONFIG = [
    {
        context: [
            "/my",
            "/many",
            "/endpoints",
            "/i",
            "/need",
            "/to",
            "/proxy"
        ],
        target: "http://localhost:3000",
        secure: false
    }
]

module.exports = PROXY_CONFIG;

and make sure to point to the right file

"start": "ng serve --proxy-config proxy.conf.js",