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.
2
Answers
Solution: Nginx proxy pass
Credit: Pierre Inglebert
NodeJS + ExpressJS - how to point my domain name to port 3000?
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.