質問

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.

ライセンス: CC-BY-SA帰属
所属していません softwareengineering.stackexchange
scroll top