Cloud Zone is brought to you in partnership with:

Daniel Mohl is a professional software engineer/architect whose interests include understanding the intricacies of various programming languages, enterprise application architecture, and how to bridge the gap between business and technology. He works with F#, C#, CoffeeScript, JavaScript, Erlang, ASP.NET MVC, WPF, WCF, Silverlight, WP7, SQL Server, etc. He is a F# MVP, C# Insider, F# Insider, blogger, speaker, and event organizer. You can follow him on twitter at www.twitter.com/dmohl. Daniel is a DZone MVB and is not an employee of DZone and has posted 30 posts at DZone. You can read more from them at their website. View Full User Profile

Getting Started with Azure, Node.js, and FogJS on Windows

12.19.2013
| 4643 views |
  • submit to reddit

About a month ago I introduced FogJS. On the main FogJS site, there are a couple of code examples that you can use to quickly start interacting with Blob Storage, Table Storage, or the Azure Service Bus. However, if you're new to using Azure from Node.js, then you may be wondering what other steps need to be done to get a dev environment up and running. While you can glean the steps by reading various How-To Guides provided on the Azure site, it can sometimes be helpful to have the process described in a slightly different way. In this post, I'll show you how to quickly get a Windows machine setup to interact with Emulator Table Storage using FogJS. I'll follow a similar structure as the Node.js Getting Started Guide and a few of the steps between this post and that guide overlap. Some of the key differences include the use of a WorkerRole rather than a WebRole, the use of the emulator, and of course the use of FogJS.

Setting up Your Windows Machine:

1. Install Node.js from http://nodejs.org/.
2. Install the Windows Azure SDK for Node.js (this may require a reboot).
3. Open a PowerShell command prompt and navigate to the parent location at which you wish to create a project. I'll use c:\Temp for this example.


4. Run the following command to create a couple of starter files for your project (note: FogJSExample should be replaced with the name of the project that you wish to create).

New-AzureServiceProject FogJSExample


6. Navigate to the FogJSExample directory and install FogJS with the command:

npm install fogjs


5. Create a worker role with the following command (note: TableStorageWorker should be replaced with the name of the worker that you wish to create).

Add-AzureNodeWorkerRole TableStorageWorker


7. Navigate to the TableStorageWorker directory, edit the server.js file, and replace the text within it with the following:
var http = require('http');
var fogjs = require("fogjs");
 
var fog = fogjs.tableStorage;
var port = process.env.port || 1337;

http.createServer(function (req, res) {
    var testTableName = "testTableName";
    var key = "myRowKey";
    var partitionKey = "testPartition";

    fog.insertEntity({
        "tableName" : testTableName,
        "entity": {
            "PartitionKey" : partitionKey,
            "RowKey" : key,
            "MyCustomField" : "Legends of Awesomeness!"
        }
    }).then(function(response){
        return fog.queryEntity({
            "tableName" : testTableName, 
	    "entityDescriptor": {  
	        "PartitionKey" : partitionKey,
	        "RowKey" : key,
            }
	});
    }).then(function(response) {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('MyCustomField from the created entity contains the text "' + 
            response.entity.MyCustomField + '"');
    }).fail(function(errorMessage) {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('There was an error with message: ' + errorMessage);
    }).then(function() {
        // For this example, we'll delete the entity. 
        fog.deleteEntity({
            "tableName" : testTableName, 
            "entityDescriptor": {  
                "PartitionKey" : partitionKey,
                "RowKey" : key
            }
        });	
    });
}).listen(port);



Published at DZone with permission of Daniel Mohl, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)