I am developing a simple application using Flask for the backend and React for the frontend. When I try to start my React application using npm start, I encounter a CORS error. However, when I use HOST=127.0.0.1 npm start, the application runs successfully without any CORS error.
Here is the code snippet for the Flask application:
#!/usr/bin/env python3
from flask import Flask
from flask_cors import CORS
from os import environ
try:
origins = environ["ORIGINS"]
except KeyError:
kwargs = {}
else:
if "," in origins:
origins = origins.split(",")
kwargs = dict(resources={r"/*": dict(origins=origins)})
print(kwargs)
app = Flask(__name__)
CORS(app, **kwargs)
@app.route("/")
def index():
return "reachedn"
if __name__ == "__main__":
app.run(port=5000)
Here is the code snippet for the React application:
import React, { useEffect, useState } from 'react';
const localhost = window.location.hostname
function App() {
const [response, setResponse] = useState('');
useEffect(() => {
document.title = localhost
fetch(`http://${localhost}:5000/`)
.then((response) => {
if (!response.ok) {
throw Error('Network response was not ok');
}
return response.text();
})
.then((data) => setResponse(data))
.catch((error) => console.log(error));
}, []);
return (
<div>
<h2>"reached" expected:</h2>
<p>{response}</p>
</div>
);
}
export default App;
I am using the hostname dynamically from window.location.hostname for the fetch URL. Does anyone know why specifying the host as 127.0.0.1 resolves the CORS error, while just using npm start does not?
2
Answers
I’ve Resolved It Myself
Method of Investigation
With the help of ChatGPT, I created the following program:
I simplified the Flask program as follows:
Then, I tried the following:
As a result, the above script output:
It seems that the AirTunes server was using port 5000.
Solution
I modified clnt.py and serv.py so that the port could be variable:
As a result, I obtained the expected output:
The front-end and back-end programs presented in the original question started working as expected after changing the port from 5000 to 5001 (provisionally).
‘npm start’ will typically start your server on http://localhost:3000 instead of http://127.0.0.1:3000 unless you specify the host. Since your backend app runs on HOST http://127.0.0.1, which is a different host from http://localhost which the react app is running, the cors middleware is blocking the request. Cross-origin requests are subject to the same-origin policy, which restricts web pages from making requests to a different origin (host, protocol, or port) unless the server explicitly allows it using CORS headers.
In this case, you’d observe that all is well when you specify the host when starting the react app as http://127.0.0.1 because the host is of same origin with that of the flask app and doesn’t violate the CORS policy.
To resolve this you will have to modify your flask code to allow request from http://localhost origin, which will look like this
OR like this if you want to allow from all origins (any host)
I hope this helps. Cheers.