Using a config file with RequireJS
It can be very useful to have a single configuration file that can be used anywhere in your JavaScript application. This is really easy to do with RequireJS.
First setup your config.js file and place it in your root JS directory:
define({
environment: 'development',
// API settings
api: {
url: 'api.someurl.com',
alwaysUseHTTPS: true,
clientID: 'xxxxxxxxxxxxxx'
}
});
Then you can use it anywhere in your code:
define(['config'], function (config) {
// Setup logging
if (config.environment === 'development') {
console.log('Running in Development Mode');
}
// App code...
});Written by Mark Wales
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Amd
Authors
gerardsans
12.4K
cgarvis
7.298K
Did you know?
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#