Corsinator is a simple Flask-based proxy server designed to handle Cross-Origin Resource Sharing (CORS) issues by forwarding HTTP requests to a target API. It supports GET
, POST
, PUT
, and DELETE
methods and handles CORS headers to facilitate cross-origin interactions.
- Forwards requests to a specified target API.
- Supports
GET
,POST
,PUT
, andDELETE
HTTP methods. - Configurable target API URL.
- Automatically sets CORS headers to allow cross-origin requests.
- Python 3.x
- Flask (
pip install flask
) - Requests library (
pip install requests
)
-
Clone the repository:
git clone https://github.com/your-username/corsinator.git cd corsinator
-
Install the required Python packages:
pip install -r requirements.txt
requirements.txt
should contain:Flask==2.3.2 requests==2.28.2
-
Configure the target URL:
Edit the
TARGET_URL
variable inmain.py
to point to your target API:TARGET_URL = 'https://api.example.com/api/v1'
-
Run the Corsinator server:
python main.py
The server will start on
http://localhost:8080
.
To integrate Corsinator with your React application, follow these steps:
-
Configure the React application to use the proxy:
In your React project's root directory, open (or create)
package.json
and add aproxy
field to point to the Corsinator server:{ "name": "your-react-app", "version": "1.0.0", "private": true, "proxy": "http://localhost:8080", "dependencies": { ... }, ... }
-
Make API requests from your React app:
In your React components or services, you can make API requests without specifying the full URL. For example:
// In a React component or service file import axios from "axios"; const fetchData = async () => { try { const response = await axios.get("/some-endpoint"); console.log(response.data); } catch (error) { console.error("Error fetching data:", error); } }; fetchData();
The proxy configuration in
package.json
will forward the request tohttp://localhost:8080/some-endpoint
, where it will be handled by Corsinator.
You can test the Corsinator server using tools like curl
or Postman, or by making API requests from your React application.
- Ensure the Corsinator server is running before making requests from the React app.
- Verify that the target URL is correctly configured in
main.py
. - Check console logs and network activity for errors.
This project is licensed under the MIT License. See the LICENSE file for details.