I am trying to run a build of a Vue app where the urls for each of my API routes will drop the localhost:3001
used for development after the build.
The problem is the production app is using nginx with reverse proxy to the production server. That is, the localhost server address is already set in the nginx
configuration and is therefor not required in the API urls in the Vue app for production.
For example a development url like this
localhost:3001/api/users
should be this is production
/api/users
Since the app does not need the localhost
part of the url is there any way to automatically remove it during the build? I may be missing something obvious but at the moment I can’t figure a way to do it without manually changing all of them.
Thanks for any guidance in advance.
2
Answers
As I understand your question, for development you are running both an API server and a UI dev server on your machine, and the UI is making calls to the API server which is running on a different port so you have to specify the host/port in the URLs. However, in production you want to not specify the host/port so it goes to the same host.
There are a couple of ways you could do this. The straight-forward way is to use environment variables in Vue. Digital Ocean actually has a guide for dealing with this exact problem:
The other way is to try and more closely mimic your production environment. You can do that by running nginx locally on your machine, have it proxy requests something like:
Then, in your /etc/hosts file (assuming a *nix OS), point dev.example.com to 127.0.0.1 and your app + API should be reachable from the same dev.example.com address.
Please check if your
nginx
configuration on the production server already contains definitions about the port 3001. If not, you can try to append this file with the following block, after modification of some values 😉As it is an extra server layer, it may be at the expense of the execution speed, but could be a temporary patch.