Steven Edouard

Developer Advocate. Tech Enthusiast. Life Enthusiast.

NAVIGATION - SEARCH

Using JSON Configuration files for Azure Mobile Services

If you log into your Azure Mobile Services (AMS) portal, you'll notice that you can now deploy your service via Git. This is an awesome feature that makes life way easier. Scott Guthrie has a really good post on it.

 

I'm used to building full fledged cloud services (with worker roles, web roles etc). With AMS, you get a bit more platform, and not as much knowledge about the infrastructure compared to other PaaS services like web and worker roles. Which is great but really became a pain when I was trying to use configuration files with my mobile services.

Most people like to keep their settings like access keys, connection strings, account settings etc. in one place. And if you're using Node.js for your mobile services (which I'm really becoming a fan of by the way!) you probably want to have some sort of JSON configuration like this:

 

{"MongodbConnectionString":"mongodb://MongoLab-4q:X7TH5Lab-4q",
 "Storage.AccountKey":"fy6fTMAFrAPNH",
 "Storage.AccountName":"mystorageaccount",
 "Storage.PhotoContainerName":"maincontainer"}



Personally I like to use nconf to read my configuration file because its really straight forward. The problem is that azure mobile doesn't provide you with very much information with the environment in which your code is running. That's the point of PaaS... isn't it? :-)

Well anyways if you follow the how-to on the nconf npm page you'll run into a problem. Say that you have a custom api like so:

nconf.argv().env().file({ file: '../shared/config.jsn' });
exports.get = function(request, response) {
    // Use "request.service" to access features of your mobile service, e.g.:
    //   var tables = request.service.tables;
    //   var push = request.service.push;
    var accountName = nconf.get('Storage.AccountName');
    console.log('Connecting to blob service account: ' + accountName);

}


And that you place your config.jsn in your shared folder under <ProjectRoot>/shared/config.jsn you'll be surprised when you look at your log output:

'Connecting to blob service account: undefined'

:-O!!

So what's going on here? It turns out (after a good amount of testing what worked and what did not) that there's something funky going on with the current executing directory. So instead, if you want to get to the configuration file, use __dirname + '/path/to/configfile.jsn'

Note: As unintuitive as it sounds, don't name your config file with the extension '.json' or azure will confuse that with a route configuration. It will break your apis with mysterious 500 errors.

So the working verison of the example above (assuming you placed your configuration file in the shared folder) is:

nconf.argv().env().file({ file: __dirname + '/../shared/config.jsn' });
exports.get = function(request, response) {
    // Use "request.service" to access features of your mobile service, e.g.:
    //   var tables = request.service.tables;
    //   var push = request.service.push;
    var accountName = nconf.get('Storage.AccountName');
    console.log('Connecting to blob service account: ' + accountName);

}




After this everything seems to be in working just fine. So to sum it up, to use json config files in your azure mobile service:

1) DON'T name the config file with the extension .json (it'll make you get unexplained 500's on calls to your apis')
2) DO use the __dirname special node.js property to make the file path absolute at run-time.


Hope this saves you some time!



'Til next time!