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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
$env:EMULATED = "true"
9. Run the following command to start the Azure Emulator (note: The emulator does not support Service Bus interaction. For working with Azure Service Bus, you will need to setup environment variables as defined here).
Start-AzureEmulator
10. Open a browser and navigate to http://localhost:81. This will cause a request to go to your Node service. When that request is received, a record is created in Table storage, then retrieved and used to populate the output that will be displayed, and finally deleted.
That's all there is to it. You can find the source for FogJS on my GitHub.