Skip to content Skip to sidebar Skip to footer

Axios Api Calls In Heroku

I've deployed my app to heroku and I have it connected to mongoLab. My problem is my API calls using axios wont't work with Heroku because they are still set up locally. Is there a

Solution 1:

You can actually remove the baseUrl option entirely and use just the relative URL '/api/saveClient'. This will work for both your dev and production environment. When you use a relative url axios will default to http://your-app-name.herokuapp.com/api/saveClient.

However, if you had to make the baseUrl explicit, the appropriate baseUrl would be: http://your-app-name.herokuapp.com. To avoid hardcoding this you could try something like:

baseUrl = process.env.baseURL || "http://localhost:3001"

Heroku lets you set environment variables in an easy fashion in their console at dashboard.heroku.com/apps/your-app-name/settings. I don't think you'll need this, but it's good to know as your app grows and you want to configure your production environment more easily.

Solution 2:

I fixed mine by just doing this:

exports.homeRoutes = (req, res) => {
  // make request to the /api/users//your-app.heroku.com/path/to/your/api
  axios
    .get("https://dashboard.herokuapp.com/api/users")
    .then(function (response) {
      res.render("index", { users: response.data });
    })
    .catch((err) => {
      res.send(err);
    });
};

In your case, this hack might work:

url:"your-app.herokuapp.com/api/saveClient"

Post a Comment for "Axios Api Calls In Heroku"