I have a Javascript frontend that does Ajax calls to my backend. To do that, it needs a "backend_URL" that I hard-coded in the Ajax get() call, say "http://myservice/backend".
Now if I want to deploy my app on different machines, some of which will use this url with HTTPS: "https://myservice/backend", and some not (because they lack a proper certificate and do not expose valuable data).
Where should I put the "USE_HTTPS=1" config variable so that someone deploying the app can choose to use or not SSL ? Of course the question extends itself to other config variables.
I thought about adding a ".config" file at the project root, but then I don't know how to import it in my code. Or should I export environment variables ? Or a node.js feature ?
I have a Javascript frontend that does Ajax calls to my backend. To do that, it needs a "backend_URL" that I hard-coded in the Ajax get() call, say "http://myservice/backend".
Now if I want to deploy my app on different machines, some of which will use this url with HTTPS: "https://myservice/backend", and some not (because they lack a proper certificate and do not expose valuable data).
Where should I put the "USE_HTTPS=1" config variable so that someone deploying the app can choose to use or not SSL ? Of course the question extends itself to other config variables.
I thought about adding a ".config" file at the project root, but then I don't know how to import it in my code. Or should I export environment variables ? Or a node.js feature ?
Share Improve this question edited Jun 5, 2016 at 11:21 JulienD asked Apr 28, 2016 at 12:26 JulienDJulienD 7,29310 gold badges55 silver badges86 bronze badges 5- It's the client-side code that needs to know this, right? – T.J. Crowder Commented Apr 28, 2016 at 12:34
- Create a file config.js and write your client side config. in it. Import it at header before the file which read config. info. – Mohammed Raja Commented Apr 28, 2016 at 12:35
- @T.J.Crowder Yes, the client-side. The server-side has Apache configured to receive either HTTP or HTTPS, but the client does not know about it, so it needs a hint. – JulienD Commented Apr 28, 2016 at 12:43
- @Mohammed Raja You mean I place it at the root of the project, and add it to <head></head> of my index.html ? – JulienD Commented Apr 28, 2016 at 12:45
- Check my ments in code section – Mohammed Raja Commented Apr 28, 2016 at 12:54
2 Answers
Reset to default 7I ended up writing a conf.js file with content
window.CONFIG = {
SOME_CONSTANT: 22,
}
and included it in a new <script>
in my index.html before the other scripts.
The window
is not mandatory but shows where it es from when I call that as window.CONFIG
anywhere in the rest of the javascript.
CONFIG = (function(){
var conf_info = {};
conf_info["url"] = 'http://codepen.io/pen/';
return{
getValue : function(param){
return conf_info[param];
}
}
})();
//some where in different file
document.getElementById("result").innerHTML = CONFIG.getValue('url');