But nuff’ talks. This is a short primer on how to deploy and run node.js app as Azure Web App. For your convinience, I put up a repo that contains the app and everything referred in this blog post. Get the code and config files with button below.
… 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.
- 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
- Go to portal.azure.com.
- Click “+ Create a resource” on top of the the left bar.
- In the search market place text box, type “web app” and hit enter
- Select Web App (plain web app) and azure shows you some details
- Hit Create – button.
Image: Create web app
Creating web app requires the following parameters:
- 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.
- Select the subscription you want to use (you most likely have only one).
- Create new resource group. Resource group is a folder in azure where all resources are put.
- In this excercise, you’re using Windows, and publishing Code.
- Disable the application insights for this demo (you don’t need advanced monitoring features just now)
- 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:
- create a new app service plan,
- closest to your location. In Finland, the closest is West Europe (Amsterdam).
- Select Dev/Test tab, pricing tier F1 (=Free)
- Hit Apply.
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, clone the sample repo, 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!
- hello.js – the application. nothing new here.
- web.config – tells web app what your app is and how to run it.
- package.json – what node modules to import, and which node file to run when started.
- 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. package.json contains the node package requirements and info which .js file should be run when starting the app.
That’s all for application and configuration. 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:
- Go to your web app instance on azure portal.
- On the top bar you have “Get publish profile” button. Click it.
- It’ll give you a file, with one piece of information you’ll need put to the publish.js file.
- Open the file from Azure, and open your publish.js source file.
Image: The publish.js
You’ll edit 2 lines (marked red in the image) in the publish.js file. Your app name (myWebAppName) and deployment password (userPWD). Your app name is the name you gave to your application. The password you have on your publish profile file (the section with publish method “MSDeploy”). Copy this in place. First run npm install to get the required packages, then you are ready to publish. Next run from commandline:
… and after the script finishes ok, your app is up and running in azure. If and when you get missing node package or wrong node version 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.
Image: It Works!
Final Tip: Kudu Console
All WebApps have a handy console interface called Kudu. To access this console, go to address (and use your Azure portal login credentials if not already logged in):
Using Kudu Debug Console, you can view and edit any of the files, and run PowerShell or CMD commands. Your app log files appear on /home/LogFiles/application directory. I’ve noticed that the node package manager (npm) isn’t perfect on Azure, and the packages may fail to deploy automatically. In this case, use kudu to fix the problem. Your app is deployed in /home/site/wwwroot dir. Go to the dir, and run npm install which usually fixes the problem.