Azure Mobile Services: available modules

Azure Mobile Services has documented a set of objects available in your Azure Mobile Services server side scripts at their documentation page Mobile Services server script reference. Although the documented list is a nice list of objects for the common things you want to do, it will be sooner than later that you will look for more functionality to be included in your script, especially with the new provided feature that you can now create your custom API’s. If you use GIT it is now possible to add any NPM module (node package manager module, say the NuGet of the node world), but why include a module if it is already available out of the box. And you can only use GIT with Azure Mobile Services if you are an administrator on your Azure Mobile Service, not if you are a co-administrator (will be solved in the future).

Until now I did some trial and error experimentation to test if a certain module was available. This is easiest to do as follows:

 

Create a custom API, for example named experiment.

In this API use the following code:

exports.get = function (request, response) {
    var module = "nonexistingmodule";
    var m = require(module);
    response.send(200, "Module '%s' found.", module);
};

You can now test your service with the following request in your browser: https://yourservice.azure-mobile.net/api/experiment

If you get the result:

{"code":500,"error":"Error: Internal Server Error"}

you know that the module does not exist. In your logs you will find the following error:

Error in script '/api/experiment.json'. Error: Cannot find module 'nonexistingmodule' [external code] at
C:\DWASFiles\Sites\yourservice\VirtualDirectory0\site\wwwroot\App_Data\config\scripts\api\experiment.js:3:13
[external code]

If you require an existing (undocumented) module like the OAuth module in the following code, you will get success as a result:

exports.get = function (request, response) {
    var module = "oauth";
    var m = require(module);
    response.send(200, "Module '" + module + "' found.");
};

If we look at the standard node.js documentation we see an extensive list of modules that can be used from your code. If we look at the list of files available in the Azure Mobile Services platform as documented in the blog post Azure Mobile Services: what files does it consist of? we see a folder node_modules with many more modules are used to build the Azure Mobile Services functionality on, but that can also be utilized from your server side node script code:

  • apn - An interface to the Apple Push Notification service for Node.js.
  • dpush - Send push notifications to Android devices using GCM.
  • mpns - A Node.js interface to the Microsoft Push Notification Service (MPNS) for Windows Phone.
  • wns - Send push notifications to Windows 8 devices using WNS.
  • pusher - Node library for the Pusher server API (see also: http://pusher.com/)
  • azure - Windows Azure Client Library for node.
  • express - Sinatra inspired web development framework.
  • oauth - Library for interacting with OAuth 1.0, 1.0A, 2 and Echo. Provides simplified client access and allows for construction of more complex apis and OAuth providers.
  • request - Simplified HTTP request client.
  • sax - An evented streaming XML parser in JavaScript
  • sendgrid - A NodeJS implementation of the SendGrid Api.
  • sqlserver – In node repository known as msnodesql - Microsoft Driver for Node.js for SQL Server.
  • tripwire - Break out from scripts blocking node.js event loop.
  • underscore - JavaScript's functional programming helper library.
  • underscore.string - String manipulation extensions for Underscore.js javascript library.
  • xml2js - Simple XML to JavaScript object converter.
  • xmlbuilder - An XML builder for node.js.

As stated before, many of these modules are used to provide the functionality of Azure Mobile Services platform, and in general should not be used directly. On the other hand, I needed OAuth badly to authenticate to the new v1.1 services of Twitter, and was very happy that a require('oauth') and a few lines of code did the job.

Based on the above modules and a lot of code in the other javascript files in the Azure Mobile Services platform a set of global objects is provided that can be used from your server side node.js script code. In future blog posts I will go into more details with respect to how this code is built-up, all starting at the node.js express entry point app.js.

No Comments