We have updated Flexmonster Software License Agreement, effective as of September 30, 2024. Learn more about what’s changed.

Change URL depending if production or development environment

Resolved
Laurier Lapointe asked on May 2, 2022

Hi,
When I deploy my project into the production environment, it is working fine when I use this URL
var boa_report = {
"dataSource":
{
Type: "api",
index: "boa-index",
url: "https://XXXXX.azurewebsites.net/api/api"
},
 
When I go back to my project locally, I need to change back the URL to this in order to make it work:
url: "http://localhost:51718/api"
 
Is there a way that it could change it automatically depending if it is running on my machine or in azure bu putting the URL in a variable or... ?
Thank you for your response and ideas.
 

3 answers

Public
Solomiia Andrusiv Solomiia Andrusiv Flexmonster May 3, 2022

Hello, Laurier!

Thank you for contacting us.
 
We want to suggest you two possible ways to determine URL automatically:

1.Use window.location to get the current URL and put it into variable as follows:

isLocal = window.location.href.includes('localhost') 
|| window.location.href.includes('127.0.0.1')
url=isLocal ? 'http://localhost:51718/api'
: 'https://XXXXX.azurewebsites.net/api/api'
var boa_report = {
“dataSource”:
{
Type: “api”,
index: “boa-index”,
url: url
},
...
}

2. Send GET request, e.x. via ajax, to the 'http://localhost:51718/api/fieldname' and check the response. Then, determine the api variable depending on the returned data in the same way as in the previous example.
 
Hope it helps. Feel free to ask if any further questions arise.
 
Regards,
Solomiia

Public
Laurier Lapointe May 4, 2022

Thank you for the suggestions Solomiia,
I will try those.
Regards

Public
Solomiia Andrusiv Solomiia Andrusiv Flexmonster May 4, 2022

Hello, Laurier!
 
Thank you for your response.
 
We will be glad to hear your feedback on the proposed approaches.
 
You are welcome to ask if any further questions arise.
 
Best regards,
Solomiia

Please login or Register to Submit Answer