Home > Net >  What is the right way to make a call when two servers run on different ports
What is the right way to make a call when two servers run on different ports

Time:02-28

I have a backend node/express app. Currently, my JWT server is running say on port 4000 and the backend is running on port 8080 (they both running on the same server). So when I want to make a request from backend server to JWT server it is quite straightforward, just put the url of the fetch request to be http://localhost:4000 and it works. However, I am a little confused for the case when this app is in Cloud Run (prod environment). I apologize if my question sounds dumb, but how do I make the similar call when the environment is production, in my case Cloud Run? Do I have to assign the public url of my my Cloud Run server (for example, https://some-backend-612y2zfwwq-uc.a.run.app/4000 or will localhost work exactly the same even in the production?

CodePudding user response:

You have 2 services (processes) and each will need to be containerized and should be containerized separately to deploy to Cloud Run, i.e. two containers, one for the backend and another for the JWT service.

It is good practice to run one process per container and because Cloud Run only permits a single TCP socket (port) per Cloud Run service, you will need to run each service as a separate Cloud Run service because each wants to publish its own port.

Cloud Run also requires that services listen on the environment variable PORT value and this defaults to 8080. So, you would also need tailor your services so that each of them would listen on whatever value is defined by PORT.

Cloud Run services may also be deployed either to require authentication or to not use authentication (i.e. be public). You will need to decide how your 2 services interact including whether e.g. your backend service authenticates to the JWT service.

There is no need to apologize for asking questions on Stack overflow. This site is for developers to ask questions.

CodePudding user response:

To get it working on a production environment, you usually expose your port on the port 80, which allows users and other applications to just make a request to whatever url you deploy your application to. For example, you usually go to google.com not google.com:3000 or whatever.

So you'd open up that port 80 to external requests and your other application would just make a call to the url of the production environment.

You technically don't have to port your port 4000 (wherever the port is deployed to 80) but that is very common.

This is also a very simple explanation of what are some common practices.

If you want to deploy your application on the same environment or network, you can check it out if it works using a docker network: https://www.tutorialworks.com/container-networking/. And you can deploy the whole network on prod where you won't really have to deal with specifying specific production urls.

  • Related