Dec 7, 2021

Avoid CORS issues in Chrome, Firefox, Safari and Edge for local JavaScript development

Setup a simple proxy using nodejs and npm to bypass CORS issues. 

This will solve the following issue in the web browsers:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disable

Install the following npm package globally:
npm install -g local-cors-proxy

Now you are ready to setup a local proxy. Let's say the URL that's throwing the CORS issue is: 
https://www.example.com/v1/api

To start the proxy, execute the following command:
lcp --proxyUrl https://www.example.com

You will get the following output:

Note: Do not close this window as long as you need the proxy.

Now, in the JavaScript code, API endpoint will be:
http://localhost:8010/proxy/v1/api

This will make a request to URL: https://www.example.com/v1/api without any issues.

Additional options to setup the proxy: 

Option Example Default
--proxyUrl https://www.google.ie
--proxyPartial foo proxy
--port 8010 8010
--credentials (no value needed) false
--origin http://localhost:4200 *

Original article can be viewed here.