Enable CORS in a React App

Dhruva
Nov 1, 2021

--

This situation applies whenever a front-end web app hosted on different domain compared to backend server, we have to deal with browser’s Same Origin policy or CORS issues. Typically you see this issue in your local environment, where in the front-end web app is served on a different port compared to backend api.

One way to by-pass CORS is via adding proxy to package.json as shown in below code.

Note: Once the above setting is enabled, restart your app and make sure when proxy is enabled, make sure you add relative paths to fetch calls i.e., (fetch(‘api/search’)) vs absolute path.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response