项目作者: das-jishu

项目描述 :
A simple Node proxy to attach CORS headers to a response of a queried request to an endpoint.
高级语言: JavaScript
项目地址: git://github.com/das-jishu/add-cors.git
创建时间: 2021-07-03T03:06:50Z
项目社区:https://github.com/das-jishu/add-cors

开源协议:MIT License

下载


add-cors

Add CORS cover image

Build Status
License: MIT
GitHub repo size
Active
Generic badge

Description: A Node proxy to add CORS headers to a request made to an API endpoint.

#

Image of error due to blockage by CORS policy


The image above shows a response blocked due to CORS policy not satisfied since correct headers were not present on the response. Frontend code won’t have access to responses if responses don’t have CORS headers embedded in it. If the backend server is owned by you, you can add the headers to solve the problem. If it isn’t owned by you, a proxy server comes into play.

#

HOW TO USE

BASE URL: https://attach-cors.herokuapp.com/

Append the base URL infront of the API endpoint and perform the request as usual. The request will be passed through the proxy to get the response. CORS headers will be added to the response and sent back.

  1. fetch("some-API-with-no-CORS") // ERROR
  2. fetch("https://attach-cors.herokuapp.com/some-API-with-no-cors") // SUCCESS

Note that it is a public proxy with rate limiting applied, so responses might be delayed. A better approach would be to create your own proxy server to handle the requests. This can be done with the following commands:

  1. git clone https://github.com/das-jishu/add-cors.git
  2. cd add-cors/
  3. npm install
  4. heroku create cors-everywhere //cors-everywhere is an example. Use a unique name for heroku to create.
  5. git push heroku master

Prerequisites: Installing the Heroku CLI

After executing the above commands, your own server would be running and available at https://cors-everywhere.herokuapp.com/
You can make requests to endpoints simply by attaching the URL infront of it.

Example: https://cors-everywhere.herokuapp.com/http://www.check-cors.com

#

EXTRAS

Feel free to add any additional features by creating a pull request.