Over a million developers have joined DZone.

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

DZone's Guide to

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

· Cloud Zone ·
Free Resource

See why enterprise app developers love Cloud Foundry. Download the 2018 User Survey for a snapshot of Cloud Foundry users’ deployments and productivity.

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

        "tableName" : testTableName,
        "entity": {
            "PartitionKey" : partitionKey,
            "RowKey" : key,
            "MyCustomField" : "Legends of Awesomeness!"
        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. 
            "tableName" : testTableName, 
            "entityDescriptor": {  
                "PartitionKey" : partitionKey,
                "RowKey" : key

Cloud Foundry saves app developers $100K and 10 weeks on average per development cycle. Download the 2018 User Survey for a snapshot of Cloud Foundry users’ deployments and productivity. Find out what people love about the industry standard cloud application platform.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}