Determining the Node.js version in Azure Mobile Services

Overview

Windows Azure Mobile Services is effectively a node.js application, and as such is extensible using node.js. Currently this extensibility comes in the form of table scripts, scheduler scripts and custom apis.

As you begin to write these scripts for your azure mobile services, it is helpful to have access not only to the documentation for the Windows Azure Mobile Services Script Reference, but to Node.js itself. Node.js however evolves rapidly as do most other modern frameworks. That means that the documentation you find on the Node.js site may be for a different version of node than is actually running in your Mobile Service.

So that brings up two questions:

  1. How do you Identify the version of Node that is running in your Azure Mobile Service?
  2. How you get docs for that version of the Node API?


How to determine the version of Node.js that is running in your Azure Mobile Service

You could actually do this anywhere that you run script in your Mobile Service. How you retrieve the value back in your client would be different for each. For me, a custom api that supports the get method is the easiest way to do both write the code, and get the result.

So, with that in mind, the first thing I’ll do is in my Azure Mobile Services management portal, in the API tab, I’ll create a custom api endpiont called "nodever" (you can call it whatever you want) and for simplicity purposes I’ll give the “GET” permission to “Everyone”:

image

Then, I’ll implement just the get method (you could do post as well) as follows:

exports.get = function(request, response) {
    response.send(statusCodes.OK, { nodeversion : process.version });
};

The script above uses the global Node.js process object to determine the version of the current Node.ls process. 

Anyhow, once I create the get method, I can simply call it from my browser using a URL (because I made sure to give the “GET” permission to “Everyone”):

http://<wamsname>.azure-mobile.net/api/nodever

You should get back a JSON string like (Note, if your browser doesn’t show the JSON but rather throws an error or prompts to save the file, check out Rick Strahl’s blog post on Displaying JSON in your Browser):

{"nodeversion":"v0.6.20"}

Getting Node.js docs for your version of Node

Great! I now know the version of node that is being used in my Azure Mobile Service. Now I can go to the node.js website, and use the version number retrieved above to get to the correct version of the docs:

General docs: http://nodejs.org/docs/<version>/ or, in my case http://nodejs.org/docs/v0.6.2/

API Docs: http://nodejs.org/docs/<version>/api/ or, in my case http://nodejs.org/docs/v0.6.2/api/

In fact, I could just update my api script to include the urls automatically:

exports.get = function(request, response) {
    var nodeinfo = {
        nodversion: process.version,
        generaldocsurl: "http://nodejs.org/docs/" + process.version,
        apidocsurl: "http://nodejs.org/docs/" + process.version + "/api/"
    };
    response.send(statusCodes.OK, nodeinfo);
};

Which would return the following JSON back to me:

{
  nodeversion:v0.6.20,
  generaldocsurl: http://nodejs.org/docs/v0.6.20, 
  apidocsurl: http://nodejs.org/docs/v0.6.20/api/
}

Final Update – Returning HTML

I just realized that since this info is supposed to be for humans, why return JSON.  I  made a quick change to my api script to return HTML rather than JSON:

exports.get = function(request, response) {
    var ver = process.version;
    var docsurl = "http://nodejs.org/docs/" + process.version;
    var apiurl = "http://nodejs.org/docs/" + process.version + "/api/";
    
    var html = "<html><head><body>Node Version: " + ver + "<br/>" +
               "General Docs Url: <a href='" + docsurl + "'>" + docsurl + "</a><br/>" + 
               "API Docs Url: <a href='" + apiurl + "'>" + apiurl + "</a><br/>" +
               "</body></head></html>" 
               
    response.type('html');
    response.send(statusCodes.OK, html);
};

Which returns this back to the client (much easier to read):

Node Version: v0.6.20
General Docs Url: http://nodejs.org/docs/v0.6.20
API Docs Url: http://nodejs.org/docs/v0.6.20/api/

Leave a Reply