CORS Anywhere helps with accessing data from other websites that is normally forbidden by the same origin policy of web browsers. This is done by proxying requests to these sites via a server (written in Node.js, in this case).
"To use the API, just prefix the URL with the API URL.". That's really all of it. So, instead of requesting http://example.com
, you will request https://cors-anywhere.herokuapp.com/http://example.com
. CORS Anywhere will then make the request on behalf of your application, and add CORS headers to the response so that your web application can process the response.
The snippet from your question automatically modifies the URL for requests generated by XMLHttpRequest
if needed. This snippet is not required, you can just prepend the CORS Anywhere API URL yourself, as done in the demo page.
The repository on Github (https://github.com/Rob--W/cors-anywhere) contains the source code of the server that powers CORS Anywhere. If you are a front-end dev, then that's all you need to know. If your application has many users, then you should host CORS Anywhere yourself, to avoid eating up all resources on the public CORS Anywhere server.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…