Building and Publishing Node.js Applications with Windows Azure Web Sites (OS X)
Overview
In this hands-on lab, you will learn the basics of the Windows Azure Web Sites service for Node.js applications. In Exercise 1, you will see how to use the Windows Azure portal for creating a web site and then publish a "Hello World" Node.js application using GIT. In Exercise 2, you will learn how to use the Windows Azure Command-Line Tools for publishing applications.
Objectives
In this hands-on lab, you will learn how to create and publish a Node.js application using:
- Git
- Windows Azure Command-Line Tools for Mac and Linux
Prerequisites
The following is required to complete this hands-on lab:
Note Note: This lab was designed to use the OS X Operating System.
Exercises
This hands-on lab includes the following exercises:
- Building and Publishing a Node.js Web Site using GIT
- Building and Publishing a Node.js Web Site using the Command-Line Tools
Exercise 1: Building and Publishing a Node.js Web Site using GIT
In the first exercise you will create a new web site from the Windows Azure portal, create a 'Hello World' Node.js application and finally publish it, taking advantage of the new GIT publishing feature provided by Windows Azure.
Task 1 – Creating a New Web Site Hosted in Windows Azure
-
Go to the Windows Azure portalWindows Azure portal and sign in using the Microsoft Account credentials associated with your subscription.

Log on into Windows Azure portal
-
In Windows Azure portal home page, click the New button located on the bottom bar.

Creating a new web site
-
Click Web Site | Quick Create. Provide an available URL for the new web site, select a region, and click Create Web Site.

Creating a new Web Site using Quick Create option
-
Wait until the new web site is created.

Creating new web site status
-
Once the web site is created click on the URL link to check that the new web site is working.

Browsing to the new web site

Web site running
-
Go back to the portal and click the web site name column to go to the site's dashboard.

Selecting the dashboard tab
-
Click the Set up Git publishing link.

Setting up Git Publishing
-
Wait a few seconds until the Git repository is ready.

Git repository created
-
Do not close the portal.
Task 2 - Creating a new Node.js Website
-
Create a new folder in a location that can be easily accessed.
-
Open your favorite text editor and copy the following code that will send the string Hello World to the browser. Save the file as server.js in the folder you have previously created.
var http = require('http');
var port = process.env.port || 1337;
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(port); -
Create a new text file and paste the following code. Save the file as package.json in the same folder.
{
"name": "HelloWorld",
"version": "0.0.1",
"description": "",
"main": "./server.js",
"engines": { "node": ">= 0.6.0" }
} Note Note: The package.json file tells the Node.js package manager (npm) how your package is structured, and which are the module dependencies. As this application is very simple it does not require dependencies, but in more complex applications you will probably need to specify a "dependencies" property. When publishing applications with Git, the Windows Azure platform automatically installs all the dependencies declared in this file.
-
In the text editor create a new file and paste the following code. This configuration indicates that the server.js file is a Node.js application that should be handled by the iisnode module.
Note Note: The iisnode project provides a native IIS 7.x module that allows hosting of node.js applications in IIS 7.x.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="false" />
<!-- indicates that the server.js file is a node.js application
to be handled by the iisnode module -->
<handlers>
<add name="iisnode" path="server.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<clear />
<rule name="app" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url="server\.js.+" negate="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="server.js" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration> -
Save the file as web.config in your web site folder.
Task 3 – Publishing the Node.js application using Git
-
Go back to the portal. Open the site's Dashboard and copy the Git Clone Url value from the bottom of the page.

Copying the Git Clone Url
-
In order to publish your local files, open a Terminal console, change directories to your application folder and run the following commands.
git init
git add .
git commit -m "initial commit"
Note Note: You can learn more about Git commands in http://git-scm.com/documentation.
-
To add the remote Windows Azure repository and push the files, run the following commands. Replace the {Git Clone Url} placeholder with the value obtained from the portal.
git remote add azure {Git Clone Url}
git push azure master -
Enter the deployment credentials when prompted.
Note Note: Deployment credentials are other than the Microsoft Account associated with your Windows Azure subscription and are valid for use with all Windows Azure web sites associated with your subscription. If you don't know your deployment credentials you can easily reset them using the management portal. Open the web site Dashboard page and click the Reset deployment credentials link. Provide a new password and click Ok.

Resetting the deployment credentials
-
Go to the site's Dashboard and click the Site Url link to ensure that the site is running. A Hello Word message will be shown.

Running the Node.js application
Exercise 2: Building and Publishing a Node.js Web Site using the Command-Line Tools
In this exercise, you will learn how manage web sites using the Windows Azure Command-Line Tools for Mac and Linux.
Note Note: These steps assume you have the Windows Azure Command-Line Tools for Mac and Linux installed.
-
Open a Terminal console and execute the following command to download your account's publish settings file. A new browser window will pop up. Log on using your Microsoft Account credentials associated with your Windows Azure subscription.
azure account download
Your publish settings will be downloaded to your browser's default download folder (usually ~/Downloads) and a new page with relevant information will be displayed.

Downloading the publish settings file
-
Go back to the Terminal console and import the file downloaded in the previous step by running the following command, specifying the publish settings file location in the {publishsettingsfile} placeholder.
azure account import {publish_settings_file} -
Execute Task 2 from Exercise 1 to create a Node.js site, but placing the files in a different folder.
-
Go back to the Terminal console and change directories in order to go to the folder where you placed the files.
-
Run the following command to create the Windows Azure Web Site with GIT publishing feature enabled.
azure site create --git
Provide a site name when prompted, for example nodejsappOSXcli. Then select a region and proceed.

Creating a new Web Site and enabling GIT publishing
Note Note: By specifying --git when running the command site create you don't need to run additional commands to initialize the Git repository or add the git remote, since these two tasks will be done automatically by the Windows Azure Command-Line Tools.
-
Now you will add the application files to a Git repository and push them to the Windows Azure Web Site. To do this, go back to the command prompt and execute the following commands. When prompted provide your deployment credentials.
git add .
git commit -m "initial commit"
git push azure master

Pushing the site files
Note Note: Deployment credentials are other than the Microsoft Account associated with your Windows Azure subscription and are valid for use with all Windows Azure web sites associated with your subscription. If you don't know your deployment credentials you can easily reset them using the management portal. Open the web site Dashboard page and click the Reset deployment credentials link. Provide a new password and click Ok.

Resetting the deployment credentials
-
Run the following command to open the published deployments for your site in Windows Azure portal.
azure site portal
In Windows Azure portal, select your site from the list and open the Deployments page of the site. Check out the latest deployment.

Site deployments
-
Execute the following command to browse to the published web site and make sure it works.
azure site browse

Web site working