I am now using the frontend with React and the backend with uvicorn.
Both are running on the same machine, but the port is different.
The main problem is that I cannot send an HTTP request to the backend while using "127.0.0.1" or "localhost".
For example,
axios({
url: "http://127.0.0.1:[BACKEND_PORT]/backend_call",
method: "post",
data: customData,
})
.catch(function (error) {
console.log(JSON.stringify(error));
})
.then(function (response: any) {
// DO SOMETHING
});
This code returns the error "ERR_CONNECTION_REFUSED" but
axios({
url: "http://MYDOMAIN:[BACKEND_PORT]/backend_call",
method: "post",
data: customData,
})
.catch(function (error) {
console.log(JSON.stringify(error));
})
.then(function (response: any) {
// DO SOMETHING
});
This returns the right response.
Also, in the uvicorn backend, host 127.0.0.1 also does not work. host 0.0.0.0 works.
Therefore, the code only works when the backend host is 0.0.0.0 and the frontend sends a request to the domain name. However, I think it is unstable and insecure. Is there any reason that I cannot send a request to localhost or 127.0.0.1?