angular-cli: Proxy not working when running ng serve

OS?

Mac OSX (Sierra)

Versions.

@angular/cli: 1.0.0-beta.31 node --version v6.9.1

Repro steps.

Need to run a proxy so I added --proxy-config proxy.conf.json in package.json This works if I run npm start but then I cant access the site from my mobile on the same network. If I run ng serve then I can use my mobile to test the site but the proxy stopps working… ?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 24 (4 by maintainers)

Most upvoted comments

Did you try

ng serve --proxy-conf proxy.conf.json

?

@kultigin trying add the below to avoid ECONNRESET error { “/emport-api/*”: { “target”: “http://localhost:8080”, “secure”: false, “logLevel”: “debug”, “changeOrigin”: true, “headers”: { “Connection”: “keep-alive” } } }

Here is my working code:

{
  "/api/*": {
    "target": "http://backend.site.example",
    "secure": false,
    "changeOrigin": true,
    "pathRewrite": {
      "^/api": "http://backend.site.example/api"
    }
  },
  "/assets/*": { // assets
    "target": "https://site.example/app",
    "changeOrigin": true,
    "secure": true
  }
}

We don’t support having the proxy-conf setting in the CLI json currently, only port/host/ssl/sslKey/sslCert. That is why it doesn’t work when you put it there.

I’ll accept it as a feature request if someone wants to take it on. Otherwise this is a consideration for 2.0 (having everything customizable in the config file).

@mackelito I added similar command line(ng serve --host 0.0.0.0 --port 4300 --proxy-config proxy.conf.json) in package.json, and follow the Proxy to backend wiki page to create a proxy.conf.json in the same folder with package.json, but it still does not work.

I am using Windows 10, I am not sure if this is a system specific problem. When I used ng eject to export webpack.config.json and configure the proxy in the devServer section, it still get the same errors in cmd console.

[HPM] GET /api/upload/uploads-1490782788752.png -> http://localhost:3000
[HPM] Error occurred while trying to proxy request /api/upload/uploads-1490782788752.png from localhost:4300 to http://localhost:3000 (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)