Angular 7 app getting CORS error from angular client

后端 未结 7 2150
逝去的感伤
逝去的感伤 2020-12-15 21:36

I have developed an angular 7 app with express backend. Express running on localhost:3000 and angular client is running on localhost:4200.

In the

7条回答
  •  长情又很酷
    2020-12-15 22:11

    To divert all calls for http://localhost:4200/api to a server running on http://localhost:3000/api, take the following steps.

    1. Create a file proxy.conf.json in the projects src/ folder, next to package.json.

    2. Add the following content to the new proxy file:

    {
        "/api": {
            "target": "`http://localhost:3000`",
            "secure": false
        }
    }
    
    1. In the CLI configuration file, angular.json, add the proxyConfig option to the serve target:
    ...
    "architect": {
        "serve": {
            "builder": "@angular-devkit/build-angular:dev-server",
            "options": {
                "browserTarget": "your-application-name:build",
                "proxyConfig": "src/proxy.conf.json"
            },
        ...
        }
    }
    ...
    
    1. To run the dev server with this proxy configuration, call ng serve.

提交回复
热议问题