gothinkster / angular-realworld-example-app

Exemplary real world application built with Angular

Home Page:https://angular.realworld.how/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CORS problem pointing to a local server

jozemi opened this issue · comments

If you want to change the API URL to a local server, in the README you can see the instructions that suggests to edit src/environments/environment.ts and change api_url to the local server's URL (i.e. localhost:3000/api)

However, if you do that, you will get a CORS error:

Access to XMLHttpRequest at 'http://localhost:8000/api/articles?limit=10&offset=0' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Maybe in the instructions will be useful to also indicate that a proxy must be configurated:

  1. Create a file src/proxy.conf.json with the content:
{
  "/api": {
    "target": "URL",
    "secure": false
  }
}

(i.e. URL = http://localhost:8000)

  1. Edit the file angular.json to include:
...
"serve": {
          ...
          "options": {
            ...
            "proxyConfig": "src/proxy.conf.json"
          },
          "configurations": {
           ...
          }
},
  1. Point to the proxy url in src/environments/environment.ts
export const environment = {
  production: false,
  api_url: 'http://localhost:4200/api'
};

close with #158