Javascript - Setting url’s according to environment

Asked

Viewed 78 times

0

My application has a login repository, very simple with pure javascript, at most a jquery.

How do I make an ajax call by passing the correct url according to the environment I’m in? If I’m in the development environment, I want to use localhost. If I’m in the approval environment, I want to use Heroku.

I thought about using dotenv to manipulate this, but since I’m using pure javascript, I can’t give a require("dotenv").

Any suggestions?

1 answer

1

Use the Location.

    var getUrl = window.location.host + '/api/login';
  • I took a closer look at your response and it would actually work if the api ran on the same web port. In my case, the api runs on one port, the webapp runs on another port and the login runs on another port. They’re different repositories on github and run separately.

  • I understood what if you try to have a url per service: var getUrlLogin = window.location.host + ':8000' + '/api/login';

  • It would be an option too. I analyzed and solved this problem with nodejs same. In my index.js, I gave a require("dotenv") and let the file settings . env resolve according to the environment. Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.