Deployment to Azure: some tips and tricks

If you have limited deployment experience and are deploying to Azure for the first time, you might encounter some common problems. Here I describe a few required steps and troubleshooting practices for deploying a node application to Azure.

I assume that you have already installed the command-line azure tool (npm install -g azure-cli), connected to your Azure account and created a new website on Azure (azure site create Site_Name --git from your app directory).

Required steps:

1) Set the NODE_ENV environmental variable to production.

azure site config add NODE_ENV=production

2) When you push to Azure (run git push azure in the terminal), Azure recreates your directory structure (in the same way as Github does) and creates a 'web.config' file. Then it looks for 'server.js' file to start a server. Typically, it looks for this file in the root directory.

If your server file has a different name or if it resides in its own folder, you need to tell Azure where to look for this file. In your 'package.json' file, add the following script:

"scripts": {
  "start": "node server/server.js"
}

In this example, the path to the server is 'server/server.js'. Replace it with your own path.

3) Do not forget to add the following line to your server file:

var port = process.env.PORT || 3000    //or any other local port;

Then use the port variable when you define the port the server is listening on.

4) For more advanced deployment practices and for installing a bower-dependent app, check out this link:
http://gregtrowbridge.com/deploying-a-bower-dependent-node-app-on-windows-azure/

Troubleshooting:

1) On the Azure portal, select your website, then go to "Configure" tab.

In app settings, make sure that NODE_ENV key has the value 'production'.

Set "EDIT IN VISUAL STUDIO ONLINE" to on.

Also, it would be useful to set "APPLICATION LOGGING (FILE SYSTEM)" to on, and choose "Information" logging level from the dropdown menu. This will help during diagnostics if you run into any problems.

Do not forget to save your changes.

2) From the "Dashboard" tab (its upper right corner), click the link "Edit in visual studio online". You will be able to see your directory structure and 'web.config' file. Check whether all the files have been uploaded correctly and node modules have been installed. Click "Run" on the left-hand sidebar. The server will run, and you will be able to see the errors log.

3) If you ever need to delete a file after it has been uploaded to Azure, you can do it from the kudu service. You can access the kudu service by going to http://sitename.scm.azurewebsites.net. From the top menu, select Debug console/PowerShell. You will be able to delete or move any uploaded files in the same way as in the terminal. Your files are located in the 'home/site/wwwroot' directory (you can check this in the "Configure" tab, Virtual applications and directories). Before deleting a file, you will have to stop the website, which can be done from Azure websites tab.