'How to make HTML form on the server to call server's localhost port
I have an Nginx server set up on my server machine. I ran my node express backend on port 3000 of server, and I was able to get my html resource (stored in my server machine as well) at www.mydomain.com/uploadImg/uploadImg.html
however, when I try to submit my html form, it doesnt call my server's localhost -p 3000.
What is the address for my backend API?
I have tried http://localhost:3000/[backend route] http://192.168.1.18:3000/\[backend route] But none of these work.
Solution 1:[1]
Unless the browser (or whatever client you are using) is running on the same computer as the server, there is no way to directly access the server's localhost IP from the browser. It is a loopback network interface accessible only to that computer.
If the server you are trying to connect to is listening on a network interface that is reachable from the browser, then you need to give the full hostname or IP address to the server in the URL. There is no syntax that allows you to write a relative URL that accesses the same host/ip with a different post. You could dynamically generate the URL using JavaScript and use location.hostname to fetch the hostname.
If the server you are trying to connect to doesn't listen on such a network interface then you can't connect to it directly from the browser. You would need to expose it somehow. For example, by changing it to listen on a different network interface or by proxying requests to it through the server that is facing the browser.
Solution 2:[2]
Solution: Nginx proxy pass
Credit: Pierre Inglebert
server {
listen 80;
server_name express.your-domain.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
} }
NodeJS + ExpressJS - how to point my domain name to port 3000?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Quentin |
| Solution 2 | KanG98 |

