Our frontend web software contains, inside a file called url.js a variable used as URL root for REST webservices.

var url = "https://devappserver:8080";

During development on developer PC, for testing purpose, the variable is often changed to

var url = "https://localhost:8080";

The problem is that often developers commits the file with variable pointing to localhost on the development branch.

We use Subversion as VCS.

Which should be the best practice to avoid the error?

Is there a tecnical solution to achieve the goal?


Here is an idea from an older Stackoverflow question. Let url.js be your default config file which is under source control. In the code which loads the variables from that file, test for the presense of another file url_local.js, which contains an override assignment like

  url = "https://localhost:8080";

Include this file if it is present using one of the mechanisms discussed here.

Do not put that file under source control. Instead, write a comment in url.js like

 // do not change the following line directly, instead do this in url_local.js
 var url = "https://devappserver:8080"

If necessary, you can also provide a file url_local_example.js in source control which everybody can use as a template for his url_local.js file.


Why not make use of environment variables? Something on your development machines with the value of DEV, and something on your servers with the value QA or TST or PRD. Then, in your code, something along the lines of

const urls = {
  dev: "https://localhost:8080",
  prd: "https://devappserver:8080"

let url;
if(envVariable === 'DEV') url = urls.dev;
else url = urls.prd;

Alternatively, you can store these sort of things in a configuration file and load the correct config file conditionally based on the environment variable.

  • Not sure if environment variable can be evaluated with JavaScript from a web page... – sgargel Feb 8 '17 at 13:17
  • True. I read this as JavaScript on the backend, not the frontend. If that's the case than my answer does not apply. – neilsimp1 Feb 8 '17 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.