Deploy Node and MongoDB app to Azure Web App on Linux and DocumentDB


Azure App Service recently added Linux option (in preview) for hosting web application on Linux natively (https://azure.microsoft.com/en-us/documentation/articles/app-service-linux-readme/).  This makes it easier to deploy Linux based solutions such as MEAN (MongoDB, Express, Angular, and Node.js) stack based web applications to Azure app service.

On a similar topic, Azure DocumentDB recently included MongoDB API support (https://azure.microsoft.com/en-us/documentation/articles/documentdb-protocol-mongodb/). This feature allows developers to use existing drivers for MongoDB to easily and transparently communicate with DocumentDB. Customers can easily build and run applications in the Azure cloud - leveraging DocumentDB's fully managed and scalable NoSQL databases - while continuing to use familiar skills and tools for MongoDB.

This article walks through how an existing Node and MongoDB application is deployed to Azure App Service on Linux, using DocumentDB as backend store.

The To Do List Sample App

The To Do List Sample App is adopted from the tutorial by Scott at: https://scotch.io/tutorials/creating-a-single-page-todo-app-with-node-and-angular.  It consists of an Angular Single Page App as front end, Node.js as service tier and MongoDB as data store.  Following changes are made to the original sample app in this lab to deploy it to Azure App Service on Linux with DocumentDB.

  1. Change the connection string to DocumentDB (from its original MongoDB)
  2. Modified the package.json file with latest versions of modules, added script section, and Node.js engine version. I also renamed server.js file to app.js.

The modified code is located at: https://github.com/harrchen/ToDo_MEAN.

To test the app, you need to create an Azure DocumentDB instance (see below), update the connection string at /config/database.js to the DocumentDB.

Run the following command from the source directory to restore package and start the app:

  • npm install
  • npm start

This is the result of the run, you can test the app from browser with URL http://localhost:3000:

localtest

Azure DocumentDB with MongoDB API Interface

To create DocumentDB with MongoDB API interface, you just need to select the option when you create the DocumentDB instance, see screenshot below.

choosemongoapi

Azure Web App on Linux

Follow this link to create a web app on Linux in Azure App Service: https://azure.microsoft.com/en-us/documentation/articles/app-service-linux-how-to-create-a-web-app/

We will use local Git repository to deploy our To Do List sample app.  Follow this link to enable the local Git repository for the created web app:  https://azure.microsoft.com/en-us/documentation/articles/app-service-deploy-local-git/.  For web app on Linux, currently only local Git repository and GitHub options are available:

selectlocalgit

Deploy Sample App to Azure

Now we are ready to deploy the application to Azure.  Follow the steps described at: https://azure.microsoft.com/en-us/documentation/articles/app-service-deploy-local-git/.  Below are the results of the deployment.

gitpush

gitpushsuccess

After deployment, it is important that you first stop the web app (from Azure portal) and then restart the web app so the new code will be executed by the Node engine.  You should see the page like below:

todowebpage

You can verify that the records are saved to the DocumentDB instance.

docdbquery

I hope this blog showed that you can easily deploy an existing Node and MongoDB application to Azure leveraging Azure Platform as a Services (Azure App Service and DocumentDB).


Comments (0)

Skip to main content