pop pop - 5 months ago 63
Node.js Question

Webpack / Angular CLI / Proxy forwarding

I need to setup a proxy within AngularCLI / Webpack env to forward requests from

http://localhost:4200/rest
to
https://someserver.com/somepath/rest


For one, the endpoint is a https and not a http.

Secondly, the request url could be
http://localhost:4200/rest/foo
or
...:4200/rest/bar
and all and any of the paths coming after '/rest' need to be mapped to
https://someserver.com/somepath/rest/foo
or
...com/somepath/rest/bar


The following proxy.conf.json doesn't seem to be configured properly:

"/rest": {
"target": "https://someserver.com",
"changeOrigin": true,
"ws": true,
"pathRewrite": {
"^/rest/": "/somepath/rest/"
},
"secure": false,
"logLevel": "debug"
}


The app is started with

ng serve --proxy-config proxy.conf.json --live-reload --host 0.0.0.0


Thanks!

Answer Source

you should change your pathRewrite

 "/rest/": {
        "target": "https://someserver.com/somepath/rest/",
        "changeOrigin": true,
        "ws": true,
        "pathRewrite": {
          "^/rest/": "/"
        },
        "secure": false,
        "logLevel": "debug"
      }