I have built & deployed my ReactJs
and NestJs
on the same server on different ports. Reactjs is running in port 3000
and Backend API is running in port 3001
.
In the env file of the ReactJs
, I have set http://localhost:3001/
as my backend API URL.
Now the problem is if I do run the backend API service from my PC from port 3001. Then the Deployed ReactJs in the production server does fetch the API from my PC’s localhost:3001 instead of my production
server’s localhost:3001
.
2
Answers
You shouldn’t hardcoded the port of the API endpoint, otherwise it will always point to the local machine where the browser is running.
In your production environment, set the environment variable to the correct URL of your backend API instead.
the API URL
localhost:3001
is only accessible to your device and it an not accessed by your production server, you need to host your back-end somewhere. then setup the server and proxy there in order to access the APIs on production server