In a previous article, I wrote about how simple it is to deploy an ASP.NET MVC Web Application to Windows Azure using Team Foundation Service hosted at tfspreview.com. In this article, I will build on those concepts and show you how to build and deploy a simple Node.js website to Azure using Git as the source code repository. One of Azure’s core strengths is its openness. In addition to the .NET SDK, Azure has SDKs for Java, PHP, Python and for the purposes of this article, Node.js. That openness also extends to Azure hosted Virtual Machines where Ubuntu, openSUSE and SUSE Linux are supported in addition to core Microsoft server technologies such as Windows Server 2012/2008 and SQL Server. In this article, I’ll provide a brief primer on Node.js, Git and how those technologies are first class citizens in Azure. One technology you will not find in this article is Visual Studio! We’ll use Notepad as our sole developer IDE!

Setting Up Your Machine

It’s quite likely you won’t have the necessary software installed to work through this article’s example. Your machine will need to have the following software components installed:

  • Node.js (nodejs.org)
  • Git (git-scm.com)

You will also need the Node.js Azure SDK, which you can download from http://www.windowsazure.com/en-us/develop/nodejs/. We will tackle that task after we’ve set up Node.js and Git and after we get our initial Node.js website up and running.

Installing and Running Node.js

Node.js is a server-side JavaScript hosting environment. It was first developed in 2009 and is based on Chrome’s JavaScript runtime. It is completely open sourced (under the MIT License). Per the documentation, “Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.” You can find Node.js’s online home at nodejs.org. There, you will find several installers, including a Windows MSI installer. The installer creates a C:\Program Files\nodejs directory and it is there you will find the node.exe file. Scrolling further down the nodejs.org homepage, you will find the canonical “Hello World” example. Let me show you all of the JavaScript code necessary to create the simple Hello World example:

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);

Even if you are not fluent with JavaScript, the simple Hello World example is very simple to follow. The server has a single function that accepts a request and response object. The request and response objects conform to the http.ServerRequest and http.ServerResponse classes respectively. You can find these classes, along with every other Node.js class, well documented in the API Documentation at http://nodejs.org/api.

To further illustrate the Node.js API, consider Listing 1, which interrogates the URL for a specific query string:

In order to parse the url and querystring, you must include the url and querystring modules in addition to the http module. If the following url is passed: http://127.0.0.1:1337/?nodejs=cool, 200 response is passed to the response. Any other url results in a 400 response. In the example illustrated in Listing 1, a query string parameter named “nodejs” is searched for and it must have the value “cool.” If that condition is satisfied, a response with a 200 status is returned with the phrase, “I agree, Node.js is cool!” in the body.

To run the Node.js Hello World example, enter the following command in the Command Prompt:

"C:\Program Files\nodejs\node.exe" server.js

The result should look similar to Figure 1.

Figure 1:  Node.js server running from the Command Prompt.
Figure 1: Node.js server running from the Command Prompt.

Once the server is up and running, you can then navigate your browser to http://127.0.0.1:1337/. Your results should look like Figure 2.

Figure 2: Node.js Hello World Results.
Figure 2: Node.js Hello World Results.

With Node.js up and running, let’s turn our attention to setting up Git.

Installing Git

Like Node.js, Git is also a fully open source tool. Git is a distributed version control system. Unlike Team Foundation Server (TFS), Git facilitates distributed repositories that can be merged to any number of remote repositories. In other words, you can freely check in, check out, branch and merge from your own private repository. Then at some designated time, you can then merge your private repository to a remote repository. TFS on the other hand, facilitates a centralized repository only. Check ins, check outs, branches and merges are only conducted against the shared centralized repository. For many, Git being a distributed version control system is the compelling reason to use Git. Git is extremely robust well supported and best of all, it’s free! The most famous public Git host is GitHub (www.github.com). To download and install Git, navigate to http://git-scm.com/ and click the download link. To learn more about Git, check out Scott Chacon’s free e-book at http://git-scm.com/book.

Once you’ve installed Git, you should have access to a new tool called Git Bash, which gives you a UNIX style command prompt from which you can run your Git commands. You can also run Git from the Windows Command Prompt. To run from the Windows Command Prompt, you need to make sure C:\Program Files (x86)\Git\cmd is in your path. For purposes of this article, I’ll continue to use the Windows Command Prompt.

Currently, there is only one file in this project: example.js. Now that you’ve installed Git, you can initialize a local repository. In the same directory as example.js, type the following command:

git init

With the repository initialized, the next step is to add the file:

git add server.js

The last step is to commit the changes to your local repository:

git commmit -m ‘initial commmit’

With Node.js and Git up and running our local website, we can now turn our attention to hosting the website in Azure. That is the name of the game, right - to make our code available beyond our local development machine!

Taking Your Node.js Site to Azure

In order to follow along from here, you need to create a Windows Azure account. If you don’t have one, don’t worry. You can sign up for a free 3-month trial. Navigate to windowsazure.com and create your account. You will also need to install the Windows Azure SDK for Node.js. You can find the various platform SDKs at:

<a href="http://www.windowsazure.com/en-us/develop/overview/";>http://www.windowsazure.com/en-us/develop/overview/<;/a> 

Clicking the node.js option will take you to the Azure node.js SDK home page. From there, you can download the node.js SDK. There is an installer for Windows, Mac and Linux. Figure 3 illustrates the installer’s summary screen. You will want to make sure the SDK is installed properly. If the SDK is not installed, although the code will deploy to the Azure, the site will not run properly.

Figure 3: Windows Azure Node.js SDK Installer.
Figure 3: Windows Azure Node.js SDK Installer.

Once you have your account and have installed the Node.js SDK, you should create an empty website. Figure 4 illustrates the new Windows Azure Portal and the Create Website UI. For this example, I selected the Quick Create option.

Figure 4: A new empty Azure-hosted website.
Figure 4: A new empty Azure-hosted website.

Figure 5 illustrates how the new website appears in Azure. At this point you just have the default home page. Soon, the website will host the Node.js website created earlier.

Figure 5: The <a href=
Figure 5: The

Once you’ve created the website, you will see the new site in the webs site list. If you click the site, the website dashboard appears. Figure 6 shows the dashboard for our nodejsexample website.

Figure 6: The nodejsexample Azure website dashboard.
Figure 6: The nodejsexample Azure website dashboard.

From the dashboard, you can then establish your publishing settings. You can choose to publish from either TFS or Git. In an earlier article, I discussed how to setup TFS publishing. For this example, we are going to work with Git. Once you click the Set up Git publishing link illustrated in Figure 6, Windows Azure will create your Git repository. Figure 7 illustrates that process.

Figure 7: The nodejsexample Azure website Git repository.
Figure 7: The nodejsexample Azure website Git repository.

One of the Azure Portal’s great strengths is in its user experience and the way it guides you to the next step. Looking again at Figure 6, you can see a number of options. In our case, we have local files in a local Git repository that we need to push to Azure. As you can see, you also have the option of pushing files from a GitHub, CodePlex or BitBucket Git repository as well. Expanding the option to push local files to Windows Azure, you are then presented with steps required to push your code to Windows Azure. Figure 8 illustrates those steps.

Figure 8: Windows Azure guides you through the steps to deploy your code!
Figure 8: Windows Azure guides you through the steps to deploy your code!

Many of these steps have already been accomplished. We have already Installed Git. In addition, we have created the local Git repository and have already committed the code. The next step is to add a remote repository to the local repository and finally, to push the code. Figure 9 illustrates the Command Prompt output from the Git deployment process.

Figure 9: Output generated from the Git deployment process.
Figure 9: Output generated from the Git deployment process.

Eventually, you will likely eschew the UI tools in favor of the command line interface. For automated unattended processes, this is a must. For more information on that, navigate to the following URL:

<a href="http://www.windowsazure.com/en-us/develop/nodejs/how-to-guides/";>http://www.windowsazure.com/en-us/develop/nodejs/how-to-guides/<;/a>
command-line-tools/

Finally, Figure 10 illustrates the deployed site. It looks like the local site, with the one notable exception being the URL.

Figure 10: The deployed Node.js website on Azure.
Figure 10: The deployed Node.js website on Azure.

Conclusion - To Node or Not to Node…

The one question you may have is when you would choose to use Node.js. Is this a replacement for other frameworks? NO! Node.js complements other tools in your toolkit. If your primary use case is a canonical CRUD app or something that is CPU intensive, Node.js is not the best choice. On the other hand, if you need a lightweight API that serves up JSON, streaming data or small/succinct operations that need to be realtime where there is a lot of concurrency, then that component of your solution may be well suited for Node.js.

From here, you will want to check out the resources at nodejs.org. There’s also the howtonode.org site which has a number of good tutorials. From the Windows Azure perspective, Azure clearly provides a platform that makes it very easy to develop and deploy solutions across a variety of technologies.

Listing 1: validatequerystring.js

var http = require('http');
var url = require('url');
var querystring = require('querystring')
var port = process.env.PORT || 1337;


http.createServer(function (req, res) {
  var parsedURL =  url.parse(req.url);
  var parsedQuery = querystring.parse(parsedURL.query)
  var queryValue = parsedQuery.nodejs;
  
   try {
     if (queryValue.toLowerCase() == "cool" ) {
       res.writeHead(200, {'Content-Type': 
'text/plain'});
       res.end('I agree, Node.js is cool!\n');
     }
     else {
       throw "bad request";
     }
   }
   catch(err) {
     res.writeHead(400, {'Content-Type': 
'text/plain'});
     res.end('I did not understand your request!\n');
   }

}).listen(port);