Technology

Using Custom NodeJS Modules with Azure Mobile Services

By July 24, 2013 4 Comments

When Microsoft first announced Azure Mobile Services and announced that the server side scripting would be using Node.js, I must admit I was sceptical. I was a Java Developer back in the days of J++ (Microsoft’s Java) and have been burned in the past of thinking they were moving to a more open ecosystem. Now that I’ve had the chance to go through AMS and get my hands dirty with the scripting, and with the more recent updates, I can say this is what I’ve been waiting for.

While the first versions of AMS did support Node.js it was somewhat limited in the fact that you could only a very small number of modules. With the introduction of git support for your scripts, this is not longer the case.

But enough of my hopes and dreams for AMS :), let’s show you how to set up your Mobile Services to allow you to use new Modules in your custom API scripts. There is lots of documentation on the Windows Azure Website if you are new to Azure Mobile Services make sure you read through that as I expect you have some familiarity with it.

Setting Up The Service

The first thing your going to want to do is create a new Custom API. For this tutorial I’ll call mine Weather and I’ll make sure that the permissions on GET are everyone (basically I want it to be a public, anonymous API endpoint).

First go to your AMS’s dashboard and click the “Set up source control” link on the bottom right. This will set things up so you can clone your scripts and work on them on your local machine (sure the web editor is nice, but Sublime Text is better 🙂 ). Then go to the configure page and copy the Git URL, and clone it on your local machine:

git clone https//blah.scm.azure-moblie.net/blah.git

You’ll find the script for your new Custom API in the file /service/api/weather.js and weather.json. We won’t be dealing with weather.json in this tutorial, but rather weather.js in your favourite editor.

For now, let’s just make sure we are dealing with Get requests, so change the first line from exports.post to exports.get and leave the rest the same. Check the change into your repo and push it back to the server:

git add .
git commit -m "Changed to deal with the Get operation"
git push

Once the push is complete you should be able to view your api in a browser by using the URL:

http://YOUR-SERVICE.azure-mobile.net/api/weather

If everything went well so far you should see the very exciting message “Hello World”.

Setting Up Node Module

Ok, now onto actually using some of the thousands of available Node modules; for this tutorial we’ll be using yql to query Yahoo’s Weather service, so the first thing to do is install the module locally using the super tool npm like this (make sure you do this in the /services/api directory or it won’t work):

npm install weather

This downloads the module and allows us to use it in our custom API, so let’s add that functionality in now by changing the script to this:

var YQL = require('yql');
exports.get = function(request, response) {
      var query = "SELECT * from weather.forecast WHERE (location = @zip)";
      new YQL.exec(query, function(data) {
response.send(200, data.query.results.channel);
      }, {"zip": 90210});
};

Once you’ve save the file you should check it in and git push it back to the server.

In this tutorial we’ve gone through and created a new Azure Mobile Service (AMS) Custom API that uses an external node module to return the caller weather data for Beverly Hills (sorry as a Canadian this is the only zip code I know 🙂 ). With this knowledge, you should be able to build any number of endpoints using the thousands of available modules.

There is a lot more to Azure Mobile Services, but as a NodeJS developer, this is one that gets me really excited. For more on what Windows Azure Mobile Services can do, check out the official website but also keep an eye on our blog as we will have more tutorials.

Join the discussion 4 Comments

Leave a Reply