Introduction

Let us look at a basic sample which retrieves the Azure table data into the applications using Node JS.

In my previous article, you have seen the basic introduction to Azure Tables.

Start creating a node application by running npm init. Create the main file (JavaScript file) for writing the core functionality. In my case, index.js will be used.

The dependency files need to be imported. To access the Azure Table Storage data, Azure Storage functions and objects will be used. The npm package called azure-storage package is available for working with Azure Storage. This needs to be imported and installed using npm command.

The following code snippet shows the package.json file created for this project.

  1. {
  2. "name": "getazuretabledata",
  3. "version": "1.0.0",
  4. "description": "",
  5. "main": "index.js",
  6. "scripts": {
  7. "test": "echo \"Error: no test specified\" && exit 1"
  8. },
  9. "author": "nakkeeran",
  10. "license": "ISC",
  11. "dependencies": {
  12. "azure-storage": "^2.5.0"
  13. }
  14. }

From the Azure Table Storage, account name and access keys need to be noted and used in the node application for accessing the Azure Storage data.

  • Navigate to https://portal.azure.com. Go to the Azure Storage account created.
  • Navigate to Access keys under Settings option. Copy the storage account name and any one of the keys available.
Main Index.js File

Create a port and import necessary packages.

  1. var port = process.env.PORT || 8000;
  2. var http = require('http');
  3. var azure = require('azure-storage');
Create the Server. In the Server function, the following steps are followed.
  1. var port = process.env.PORT || 8000;
  2. var http = require('http');
  3. var azure = require('azure-storage');
  1. var accountName = "accountname copied from azure portal";
  2. var accessKey = "access key copied from azure portal";
  1. var server = http.createServer(function(request, response) {
  2. //response.end("Listening on port %s...", server.address().port);
  3. var tableService = azure.createTableService(accountName,accessKey); // explicit
  4. var TableQuery = azure.TableQuery;
  5. var TableUtilities = azure.TableUtilities;
  6. var queryCondition = TableQuery.stringFilter('PartitionKey', TableUtilities.QueryComparisons.EQUAL, "Grade 1");
  7. var query = new azure.TableQuery().where(queryCondition);
  8. tableService.queryEntities('azuretesttable', query, null, function(error, result, res) {
  9. if (!error) {
  10. response.end(JSON.stringify(result));
  11. }
  12. else{
  13. console.log(error);
  14. }
  15. });
  16. });

The following code snippet shows the entire main function logic.

  1. var port = process.env.PORT || 8000;
  2. var http = require('http');
  3. var azure = require('azure-storage');
  4. var accountName = "accountname copied from azure portal";
  5. var accessKey = "access key copied from azure portal";
  6. var server = http.createServer(function(request, response) {
  7. var tableService = azure.createTableService(accountName,accessKey);
  8. var TableQuery = azure.TableQuery;
  9. var TableUtilities = azure.TableUtilities;
  10. var queryCondition = TableQuery.stringFilter('PartitionKey', TableUtilities.QueryComparisons.EQUAL, "Grade 1");
  11. var query = new azure.TableQuery().where(queryCondition);
  12. tableService.queryEntities('azuretesttable', query, null, function(error, result, res) {
  13. if (!error) {
  14. response.end(JSON.stringify(result));
  15. }
  16. else{
  17. console.log(error);
  18. }
  19. });
  20. });
  21. server.listen(port);
The following snapshot shows the results retrieved from the Azure Table.

Summary

In this article, you have seen the basic example of retrieving the Azure Storage Table data into Node.js application.