1

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?

S. Yong
  • 79
  • 1
  • 5
  • The issue is most likely how you are listening. Do you have a node app or something listening on that port? What does MYDOMAIN resolve to? – Matt Oestreich Aug 04 '22 at 15:27
  • MYDOMAIN is the domain of the server machine. – S. Yong Aug 04 '22 at 15:30
  • For example, when the server machine has IP of 12.23.45.67 and has domain of mydomain.com, the request failed when I send to 127.0.0.1 but succeeded when I send to mydomain.com – S. Yong Aug 04 '22 at 15:32
  • And you are logged into the 12.23.45.67 machine when you try to send to 127.0.0.1? I believe the issue is with how the app running on [BACKEND_PORT] is listening. It seems to be listening on port 12.23.45.67 and not loopback. Did you write the app that is listening? – Matt Oestreich Aug 04 '22 at 15:36
  • In this case, the server is not a local machine. Therefore, I access to "mydomain.com:[FRONTEND_PORT]" through web browser, and the browser sends a request to a backend process. – S. Yong Aug 04 '22 at 15:43
  • If you are sending the request to the 12.23.45.67 machine via 127.0.0.1 while you're actually logged into that machine, that's why it's not working. 127.0.0.1 is the loopback address and resolves to the current machine you're logged into. – Matt Oestreich Aug 04 '22 at 15:49

0 Answers0