Aniruddha Bera
Aniruddha Bera

Reputation: 415

Replace localhost in heroku

Going to deploy an application on heroku. I have got requests from react-axios that calls to localhost. Now I know that when I deploy it to a server, localhost refers to server address.

So how do I change these localhost to IP assigned by Heroku.

Here is an example of axios call from react

axios.get("http://localhost:5000/getMessages").then((response) => {
      this.setState({ res: response.data });
}

Upvotes: 1

Views: 3617

Answers (3)

axios.get('heroku deploy address/getMessages')

Upvotes: 0

yaya
yaya

Reputation: 8253

Change:

axios.get("http://localhost:5000/getMessages")

to:

axios.get("/getMessages")

Upvotes: 3

Michał Lach
Michał Lach

Reputation: 1278

Since you are hosting your app on heroku, and you are calling your API that is on the same domain, you can use window.location.hostname

axios.get('${window.location.hostname}:5000/getMessages').then((response) => {
      this.setState({ res: response.data });
}

Upvotes: 1

Related Questions