expressherokuenvironment-variablesaxiosbase-url

Heroku adding 'undefined' to BASE_URL environment variable


attempting to deploy a vuejs, express app to heroku. The app displays, but cannot access the api, because heroku seems to add 'undefined' in the middle of the base_url. Everything works locally as expected.

Here is my heroku config var: BASE_URL: https://goore-todos.herokuapp.com/

Here is the Vuejs component api request:

fetchTodo () {
  let uri = process.env.BASE_URL + '/api/all';
  axios.get(uri).then((response) => {
    this.todos = response.data;
  });
},

As mentioned, this works locally.

the console shows the following error: VM71:1 GET https://goore-todos.herokuapp.com/undefined/api/all 404 (Not Found)

and the view is empty.

requests to https://goore-todos.herokuapp.com/api/ via Postman work as expected.


Solution

  • In this case it looks like process.env.BASE_URL is undefined. As it is undefined, the url you're trying to access is considered to be relative. That means it uses your current domain and appends the path to it.

    If your frontend and backend are running on the same domain there is no reason to try to pass the API url as a variable as you can just use relative URLs.

    If you want to access the URL via an environment variable it is a bit trickier with a frontend app. The Vue app is running on the user's browser, not on your server so you can't directly access it.