After a recent domain change at my workplace one of the API endpoints became inaccessible due to some configuration problems resulting in incorrect behavior in regards to cross-origin resource sharing policy. It interfered with one non-critical function of the website and was a bit of a nuisances but not big enough for back-end team to prioritize fixing it.

Thankfully, one of my colleagues from the front-end team discovered a workaround the issue which I was not aware of before. Apparently, there is a service called CORS Anywhere which is a simple API that enables cross-origin requests to anywhere. It works by proxying requests to these sites via a server.

Thus, all you have to do to work around CORS is to prepend the URL you want to access with https://cors-anywhere.herokuapp.com/ and spoof an origin header. For example, instead of writing axios.get('https://example.com') you would write as below:

This makes a call to https://example.com with origin header set to the one that satisfies CORS policy requirements, and https://cors-anywhere.herokuapp.com returns us the result. Simple yet elegant solution.

If you don’t want to rely on a 3rd party, you can also set up CORS Anywhere on your machine using npm module cors-anywhere. Of course, at this stage you may just as well set up your own proxy on your backend but if for whatever reason you don’t want to do that, keep this option in mind.

What do you think?

I hope you have got, how to use CORS Anywhere. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Arif Khoja
Arif Khoja is a Developer. He is a Javascript Enthusiatic who loves logical programming and has first hand experience in building a cutting edge internet product using Angular. He is also an open source freak and keen about learning and sharing. He writes Javascript both frontend and backend. He loves learning and sharing tech all the time. He also has a hands on experience in SEO and writes articles about latest emerging technologies.
Arif Khoja on FacebookArif Khoja on GoogleArif Khoja on InstagramArif Khoja on LinkedinArif Khoja on Twitter