Skip to content

Add a Proxy to browsersync

gforceg edited this page Mar 31, 2017 · 12 revisions

If you have an API Server running on a different port, you may want to proxy requests from you application to your api.

You can do this by adding a middleware such as http-proxy-middleware.

To use http-proxy-middleware, you'll first need to install it:

npm install http-proxy-middleware --save-dev

Then open the file tools/config/project.config.ts.

As the default browsersync config is defined in tools/config/seed.config.ts we would extend it in our project specific settings (tools/config/project.config.ts):

Override the PROXY_MIDDLEWARE configuration variable. The example below will proxy all routes that begin with '/api' to a web api server running on port 5000:

 /* Add proxy middleware */
 this.PROXY_MIDDLEWARE = [
   require('http-proxy-middleware')('/api', { ws: false, target: 'http://localhost:5000' })
 ];

The http-proxy-middleware project has more examples of how to use it with browsersync.