Answer the question
In order to leave comments, you need to log in
How to correctly form api url on the frontend?
Good afternoon! Interesting opinion of experienced people.
How is it more correct to set api url on the frontend , relative to the domain, or set a variable in packege.json and then form api url relative to it in the code ?
For example, two options:
1. In code
const getApiUrl = () => {
if (document.domain === 'staging.com') {
return 'http://staging-api.com';
} else if (document.domain === 'prod.com') {
return 'http://prod-api.com';
}
return 'http://dev-api.com';
};
"buid:staging" "cross-env --env.API_VARIABLE=staging",
....
const apiUrl = {
staging: 'http://staging-api.com',
prod: '...'
dev: '...'
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question