简体   繁体   中英

JavaScript development and production mode

What is the best way to setup javaScript app settings for local (development) and production mode. Ex, I have different REST API links on my local machine and on the server. I can solve it by moving this setting into a different settings file and gitignore it. It there any other possible solution for it? var debug mode = true? What is the best practice? Thanks!

As you've already noted, one option is simply to have:

window.IS_DEBUG = true;

somewhere and then check:

if(window.IS_DEBUG){ ...

Rather than hard-coding it though, another option would be to calculate it on load, by looking at the URL. For example:

$(function() {
    window.IS_DEBUG = window.location.indexOf('localhost') != -1;
    // Start the rest of your code
});

The advantage of this later approach is that you don't need to git-ignore any files (or have separate files at all).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM