Announcing Node.js support for Azure Mobile Apps

September 9, 2015 jason Uncategorized 0 Comments

Last month, we announced significant update to the Mobile Apps feature of Azure App Service, that enabled developers to quickly build mobile backend APIs in .NET, add mobile features to their existing ASP.NET web apps, as well as add web pages to their .NET mobile backends. Today we are releasing a Node SDK for Azure Mobile Apps, extending similar experience to Node.js apps. This enables you to build and run mobile backends using Node.js on App Service, as well as add push notifications, mobile auth, offline sync and other mobile features and backend APIs to any Node.js app running on App Service. This SDK is released as Open Source under the MIT license and we welcome contributions from our community.


Getting Started with Node and Azure Mobile Apps


To get going, we’ll be following the short and easy quickstart steps included in the readme.


  1. Create an Azure Mobile App. You can do this by following the instructions found here. You can also use a Web or API App, as the Mobile App experience is available in API, Mobile, and Web Apps.

  2. Create a new directory, initialize git, and initialize npm


    
    mkdir quickstart
    git init
    npm init
    

  3. Install (with npm) the azure-mobile-apps and express packages


    
    npm install express azure/azure-mobile-apps-node --save
    

  4. Create a server.js file and add the following code to the file:


    
    var app = require('express')(); // Create an instance of an Express app
    var mobileApp = require('azure-mobile-apps')(); // Create an instance of a Mobile App with default settings
    
    mobileApp.tables.add('TodoItem'); // Create a Table for 'TodoItem' with default settings
    
    mobileApp.attach(app); // Attach the mobileApp to express
    app.listen(process.env.PORT || 3000);
    

  5. Run your project locally with by executing node server.js

  6. Publish your project to your existing Azure Mobile App by adding it as a remote and pushing your changes.


    
    git remote add azure https://{user}@{sitename}.scm.azurewebsites.net:443/{sitename}.git
    
    git add package.json server.js
    
    git commit -m 'Quickstart created'
    
    git push azure master
    

For steps 4-5, you can use any of the clients found in the Client & Server Quickstarts to test. To test locally, you’ll need to set the client to point to https://localhost:3000.


Did you notice that we’re just using express to run the site? This is an example of the benefits of Web and Mobile Apps being one cohesive experience. This means that it deploys like a Web App and can use features like deployment slots and WebJobs.


Using Mobile Apps Tables


Mobile App Tables is a layer on top of your data store that allows the Azure Mobile Apps clients to access your application’s data. Today we support Azure SQL DB and SQL Server for production applications. We’ve also included an in-memory store which shouldn’t be used for production purposes, but can be convenient for demos and development purposes. When you add a table to your Mobile App, on the first request, the SDK will create, in the case of SQL Server, a table. The table will have some important columns for the SDK like the id and a timestamp. It will also have any columns specified in the table definition, and if you have Dynamic Schema enabled, it will also add any columns included in the body, but not in the table already.


While being able to add a table with a single line of code is pretty cool, what if we want to customize the CRUD operations? There is a handy table configuration object to make that easy. The best practice is to define this in a separate file in a ./tables directory, so we’ll create a TodoItem.js file.



// TodoItem.js
var todoTable = require('azure-mobile-apps').table('TodoItem');
// Export our table
module.exports = todoTable;
//todoTable.columns = {"text":"string", "complete":"boolean"} //Dynamic schema will create these for us. It's on by default.
todoTable.dynamicSchema = true; // this is the default setting

todoTable.read(function(context){
  return context.execute();
});


There is support for the 4 classic CRUD operations with corresponding methods named insert, read, update, delete. You can read more about the table operations in our API Documentation.


The context object exposes different properties depending on the operation; read expose a query object and insert and update expose an item object. These allow you to modify the behavior of the CRUD operation from the default.



// TodoItem.js continued
// Attach a user id to the item that was inserted
table.insert(function (context) {
  context.item.user = context.user.id;
  return context.execute();
});

// Only return items where the user id matches the current user
table.read(function (context) {
  context.query.where({
    user: context.user.id
  });
  return context.execute();
});


To include our TodoItem table (and any other table in the ./tables directory), we just add the import line in our server.js file.



// server.js
var app = require('express')(); // Create an instance of an Express app
var mobileApp = require('azure-mobile-apps')(); // Create an instance of a Mobile App with default settings

mobileApp.tables.import('./tables'); // Import tables from './tables'

mobileApp.attach(app); // Attach the mobileApp to express
app.listen(process.env.PORT || 3000);


If you haven’t specified a database in your app settings (which the portal will help you do), it will use an in memory store which should not be used for production purposes. Currently, the SDK only supports SQL Server, but we’re investigating other options and we’re open to contributions in this area. Visit our github page to learn about setting up local environment variables to enable SQL locally.


Using Auth with Mobile AppsĀ and Node


Your next question is probably related to that context.user object. How does that get there? That’s part of Azure Mobile Apps integrated authentication features. To learn more about how to set up auth, you can follow our Getting Started with Auth tutorial. Once you’ve done that, you can require authentication for a given table action with the following code.



table.insert.auth = true;


If you want the whole table secured, you can just use:



table.auth = true;


Setting up auth to work locally requires a few extra steps that we’ll cover later on.


Feedback and Thanks


A special thanks goes out to all the community members that have helped us with early feedback prior to the release. We’d like to encourage all our users to provide us with any feedback you have on the product or the experience of using the product. Feel free to leave an issue on our github for any bugs or new features. Push will be coming soon and we’ll have more blogs and documentation coming as we keep fleshing out the SDK prior to release, so keep an eye out and start building some apps!


For more news and information, checkout these links:



Source: Microsoft Azure News

Could not resolve host: urls.api.twitter.com