Over a million developers have joined DZone.

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

· Cloud Zone

Download the Essential Cloud Buyer’s Guide to learn important factors to consider before selecting a provider as well as buying criteria to help you make the best decision for your infrastructure needs, brought to you in partnership with Internap.

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



The Cloud Zone is brought to you in partnership with Internap. Read Bare-Metal Cloud 101 to learn about bare-metal cloud and how it has emerged as a way to complement virtualized services.

Topics:

Published at DZone with permission of Daniel Mohl, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}