Run node.js apps on Azure

//Run node.js apps on Azure

Run node.js apps on Azure

I’m often asked if node.js applications can be deployed and run on Azure. Short answer: Yes, you can. If you hadn’t already noticed: Azure isn’t only about Microsoft tech anymore. You can use Java, Javascript (node.js), PHP, Python and even C# to write your apps. Services include all the popular open source tools like maria/mysql, postgreSQL, redis, docker and kubernetes just to mention few. Using Azure, you even get bonuses like an actual SLA for service uptime, ability to connect to on-premise resources, compliance, enterprise class monitoring and security features (Microsoft takes security and trust very seriously). Functions, storage, chatbots, machine learning – everything you can expect from big players like Microsoft. All just a button click away for you to use.

But nuff’ talks. This is a short primer on how to deploy and run node.js app as Azure Web App. For your convinience, here’s the zip bundle that contains the app and everything referred in this blog post.

The application

… is of course node.js hello world web app, so we don’t get distracted on the main topic on how to make it run on Azure.

Azure Web App

Web App is your Azure runtime where you publish and run your app in. As a short background: Web app can be used to run a variety of app types: You can deploy java apps, php, python, C# apps, even plain static web sites to a web app. This time we’ll be using it to run node.js app. Paid web apps have an SLA of 99,95% out of the box, and you can set it to automatically scale out to multiple instances when the load gets high. For more complete description about web app features, click here.

Deploying node.js app begins by creating a web app on azure. The thing people both love and hate in Azure, is that you can reach the same goal in different ways. You can do all this stuff directly from commandline (== you can automate), and if this is your taste, here are the instructions to do just that. On this blog I’ll show you how to do it from azure portal, just to get you familiarized with Azure.

Prerequisites:

  • You have an azure subscription (you can get free trial from here if you need)
  • You have enough rights for your subscription to do things (like owner or co-admin)

Creating a new web app on azure portal

  1. Go to portal.azure.com.
  2. Click “+ Create a resource” on top of the the left bar.
  3. In the search market place text box, type “web app” and hit enter
  4. Select Web App (plain web app) and azure shows you some details
  5. Hit Create – button.

Image: Create web app

Creating web app requires the following parameters:

  1. Give the application a name (this will also be the url address of your app). The name must be globally unique, as it will become [appname].azurewebsites.net – kind of url.
  2. Select the subscription you want to use (you most likely have only one).
  3. Create new resource group. Resource group is a folder in azure where all resources are put.
  4. Select or create new app service plan, see below

Image: Web app name, resource group and app service plan.

This is a good place to explain a little. App service plan defines on which kind of compute resource your app will be run, and where it will be run. Basically you are choosing between computer types (memory, cores), and features you need scaling etc. And of course price. For this little excercise:

  1. create a new app service plan,
  2. closest to your location (in Finland, the closest is West Europe (Amsterdam).
  3. Select pricing tier Free.
  4. Hit ok.

Image: App service plan – select Free.

Now you are ready, hit Create. Azure starts creating your application, will take a few minutes. The little bell icon on the top bar will notify you your web app is done. After the deployment finishes, you can already test that web app is up and running.  Your url is shown in the web app Overview section.

The app, and the other files

If you hadn’t already done so, unpack my source folder zip, which contains the needed files. The following screenshots are taken from my Visual Studio Code, but you can use any editor you like & command line tools you are used to.

Image: Folder contents and the hello app!

Folder contents:

  • hello.js – the application. nothing new here.
  • iisnode.yml – for configuring your node environment.
  • web.config – tells web app what your app is and how to run it.
  • publish.js – we use this to publish your app to azure.

Open up the web.config file. This file contains 2 things. First, it contains the rewrite rules for url requests. We define that all incoming requests are redirected to our hello.js file. Secondly, it sets up a thing called iisnode, which is a bunch of definitions on how to run node.js apps on webapp.

iisnode.yml is not by definition actually a required file, more just a convention. This file overrides the iisnode definitions made in the web.config, and is commonly used to configure the node.js runtime environment.

That’s all for application and configuration. The rest you are already familiar with – package.json, node libs etc.

Next: How to publish.

Publish to Azure

I’ll be honest: Most people use Local Git push to publish node.js apps to azure. And here are instructions on how to do that if you like. I like to use separate publish.js script, because I’m usually working alone, am not that big of a GIT fan, and like things happening fast. And I’m kind of used to having “Publish” button to do the thing. So here’s how it goes with node.js app:

  1. Go to your web app instance on azure portal.
  2. On the top bar you have “Get publish profile” button. Click it.
  3. It’ll give you a file, with 2 pieces of information you’ll need put to the publish.js file.
  4. Open the file from Azure, and open your publish.js source file.


Image: Get Publish Profile


Image: The publish.js

You’ll edit 3 lines in the publish.js file. Your app name, deployment username and deployment password. (see the image). Your app name is the name you gave to your application. The rest two you have on the publish profile file. Copy these in place. And you are ready to publish. Next run from commandline:

node publish.js

… and after the script finishes ok, your app is up and running in azure. If and when you get missing node package errors, just fix them as they come along as usual. After the script goes through, go ahead and test your app! The app url can be found on the web app overview section in Azure portal.

Congratulations. You’ve done it.

Quick developer tip: If you want to see your app logs, or mess with the deployment on file level, web apps always have kudu-console available for this, goto:

https://[yourappname].scm.azurewebsites.net

A few good links to go:

By |2018-02-09T14:57:09+00:00February 8th, 2018|Categories: Modern apps|0 Comments

Leave A Comment