Zum Hauptinhalt wechseln

 Subscribe

Last month, we announced significant update to the Mobile Apps feature of Azure App Service, which 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, follow the short and easy quickstart steps included in readme.

  1. Create an Azure Mobile App using 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 by executing node server.js.

  6. Publish your project to the 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, set the client to point to https://localhost:3000.

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

Using Azure 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. 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 four classic CRUD operations with corresponding methods named insert, read, update, delete. 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), 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 Azure Mobile Apps and node

Your next question is probably related to that context.user object. How does it get there? It'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.authorise = true;

If you want the whole table secured, use:

table.authorise = true;

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

Feedback and thanks

A special thanks goes out to all the community members who have helped us with early feedback prior to this release. We'd like to encourage all of our users to provide any feedback you have on the product or experience of using it. 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 too 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:

  • Explore

     

    Let us know what you think of Azure and what you would like to see in the future.

     

    Provide feedback

  • Build your cloud computing and Azure skills with free courses by Microsoft Learn.

     

    Explore Azure learning


Join the conversation