0

I am running into a CORS issue when trying to have my React app send post requests to an api endpoint while using localhost:3000.

How do I configure my app to use custom.domain.dev instead of localhost:3000?

I have /etc/hosts setup to to include 127.0.0.1 custom.domain.dev as well as my .env file to include HOST=custom.domain.dev.

As noted in this stackoverflow question,

Chrome does not support localhost for CORS requests (a bug opened in 2010, marked WontFix in 2014).

kayq
  • 133
  • 2
  • 12

1 Answers1

0

I think there are two parts of the question -

Regarding the CORS flag on local host

CORS flag is raised by the Browser when there is a request from a domain/port to an entire new domain/port. If you are doing this for testing, you can disable this security flag in chrome by adding the --disable-web-security flag. Just create a shortcut of chrome to desktop > Right click > Properties >In shortcut tab - Target > Append --disable-web-security --user-data-dir="C:\tmpChromeSession" to target. This will disable the CORS check.

In real cases, if you have access/control on the api server, what you should be doing is to enable CORS on server by adding necessary response headers (Access-Control-Allow-Origin) to the response. If you do not have access, one option will be to route the request through a CORS proxy.

Change port

You can change the running port of the react app on the package.json See this Stack Overflow answer. If you run the app on your machine, you are stuck with localhost.

Mukesh Keshu
  • 467
  • 2
  • 13