1

I am using express Node backend and frontend for angular. the thing is when i was integrate my API to angular i had CORS problem. inside the developer console i have got a error like this "XmlHttpRequet cannot load http://localhost:3000/student No Access-Control-Allow-Origin"

I have tried to proxy the backend adding a proxy-confg.json inside the angular project. but the result also same. still have CORS problem.

// node js endpoint
ApienpointUrl = 'http://localhost:4200/stdApi/student';

// getting data from endpoint
getStudentfromMock() {
return this.http.get(this.ApienpointUrl).pipe(
  map((res: any) => res),
  catchError(this.handleError)
);
}

Here the Proxy configuration inside angular project proxy.conf.json

{
  "/stdApi/*": {
    "target": "http://localhost:3000",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true
  }
}

i am getting error like this:-

"XmlHttpRequet cannot load http://localhost:4200/stdApi/student No Access-Control-Allow-Origin. header is present on the request resources origin 'http://localhost:3000' therefor not allowed access".
1
  • Verify well the Api Endpoint url , maybe you have more prefixes (like stdApi/v1 ), as a suggestion, verify the apiEndpoint in your environement.ts : instead of 'localhost:3000' is should be 'localhost:3000/stdApi' ;) Commented Feb 8, 2021 at 17:54

3 Answers 3

2

For proxying to backend server, after adding proxy configuration in proxy.conf.json in your project's src/ folder, add the proxyConfig option to the serve target, in the CLI configuration file, angular.json:

...
"architect": {
  "serve": {
    "builder": "@angular-devkit/build-angular:dev-server",
    "options": {
      "browserTarget": "your-application-name:build",
      "proxyConfig": "src/proxy.conf.json"
    },
...

Then run dev server with this proxy configuration, call ng serve.

OR

Run ng serve with proxy configuration:

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

More info: https://angular.io/guide/build#proxying-to-a-backend-server

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks mate. Yes you are right. but i have just modify the package.json and run the project using npm start. this is also work well. "scripts": { "ng": "ng", "client": "ng serve --proxy-config proxy.conf.json" }
Yeah. You can do it with scripts property in package.json too. :)
1

I have found another way to fix this CORS. here what it is.

Rewriting the URL path

{
  "/api": {
    "target": "http://localhost:3000",
    "secure": false,
    "pathRewrite": {
      "^/api": ""
    }
  }
}

This also work for me.

2 Comments

This is were i have found the answare. link
The link you provided is broken. Can you update it?
0

Add this code in app.js file.

app.use((req, res, next) => {
   // Website you wish to allow to connect
   res.setHeader('Access-Control-Allow-Origin', '*');

   // Request methods you wish to allow
   res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

  // Request headers you wish to allow
  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type, token, language');

   // Set to true if you need the website to include cookies in the requests sent
   // to the API (e.g. in case you use sessions)
   res.setHeader('Access-Control-Allow-Credentials', true);

   // Pass to next layer of middleware
   next(); 
});

And restart the server.

3 Comments

Bypassing CORS will be a security threat. You can read more here: codecademy.com/articles/…. Angular always recommends using proxy server for avoiding CORS issue on development.
How to pass API Key in the header?
res.setHeader('Access-Control-Allow-Origin', '*'); Browsers no longer accept * as a valid Origin since 2018

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.