Azure SDK for Node.js - Documentation
Перейти к файлу
Chris Tavares 216c4757a6 Updated to latest packages, generator, & nuget.exe 2014-02-11 12:12:04 -08:00
examples Add a few missing copyright headers 2013-11-08 14:34:41 +08:00
jsdoc Updating grunt & grunt-jsdoc versions, fixing up jsdoc.json, and removing reference to non-existant template file until when/if we settle on a template. 2014-01-13 11:27:29 -05:00
lib Updated to latest packages, generator, & nuget.exe 2014-02-11 12:12:04 -08:00
scripts Hooked resourceManagement into setup script 2014-01-17 13:57:17 -08:00
tasks Updated to latest packages, generator, & nuget.exe 2014-02-11 12:12:04 -08:00
test Merge pull request #1151 from WindowsAzure/master 2014-02-06 11:20:13 +00:00
.gitignore Updating grunt & grunt-jsdoc versions, fixing up jsdoc.json, and removing reference to non-existant template file until when/if we settle on a template. 2014-01-13 11:27:29 -05:00
.jshintignore Add scheduler clients 2013-12-27 16:18:12 +00:00
.jshintrc Allowing shadow for now 2014-01-14 00:04:56 +00:00
.npmignore Add scheduler clients 2013-12-27 16:18:12 +00:00
.travis.yml Allow 0.6 failures 2014-01-10 14:55:51 +00:00
CONTRIBUTING.md Adding contributing file 2013-04-20 21:42:08 +01:00
CONTRIBUTORS.txt adding contributors file 2013-11-08 16:08:26 +08:00
ChangeLog.txt update version and changelog 2014-01-29 22:57:08 +01:00
LICENSE.txt 0.5.0 release. 2011-12-09 15:47:08 -08:00
README.md address PR feedback 2014-01-10 10:17:35 -08:00
gruntfile.js Updated to public sdk 2014-02-11 11:45:47 -08:00
package.json Updated version # for subscriptions package. 2014-02-07 10:14:38 -08:00
packages.config Updated to latest packages, generator, & nuget.exe 2014-02-11 12:12:04 -08:00

README.md

Windows Azure SDK for Node.js

NPM version Build Status

This project provides a Node.js package that makes it easy to consume and manage Windows Azure Services.

Features

Getting Started

Install from npm

We provide both fine-grained modules for different Windows Azure services which you can install separately, and an all-up module which contains everything.

Notice: we haven't provided fine-grained modules for every supported Windows Azure services yet. This will come soon.

Install the all-up module

npm install azure

Install the fine-grained modules

  • Core management: npm install azure-mgmt
  • Compute management: npm install azure-mgmt-compute
  • Web Site management: npm install azure-mgmt-website
  • Virtual Network managment: npm install azure-mgmt-vnet
  • Storage Account management: npm install azure-mgmt-storage
  • SQL Database management: npm install azure-mgmt-sql
  • Service Bus management: npm install azure-mgmt-sb

Usage

Table Storage

To ensure a table exists, call createTableIfNotExists:

var tableService = azure.createTableService();
tableService.createTableIfNotExists('tasktable', function(error){
    if(!error){
        // Table exists
    }
});

A new entity can be added by calling insertEntity:

var tableService = azure.createTableService(),
    task1 = {
        PartitionKey : 'tasksSeattle',
        RowKey: '1',
        Description: 'Take out the trash',
        DueDate: new Date(2011, 12, 14, 12)
    };
tableService.insertEntity('tasktable', task1, function(error){
    if(!error){
        // Entity inserted
    }
});

The method queryEntity can then be used to fetch the entity that was just inserted:

var tableService = azure.createTableService();
tableService.queryEntity('tasktable', 'tasksSeattle', '1', function(error, serverEntity){
    if(!error){
        // Entity available in serverEntity variable
    }
});

Blob Storage

The createContainerIfNotExists method can be used to create a container in which to store a blob:

var blobService = azure.createBlobService();
blobService.createContainerIfNotExists('taskcontainer', {publicAccessLevel : 'blob'}, function(error){
    if(!error){
        // Container exists and is public
    }
});

To upload a file (assuming it is called task1-upload.txt and it is placed in the same folder as the script below), the method createBlob can be used. This method will return a writable stream which can be writen to, for instance, through piping:

var blobService = azure.createBlobService();

fs.createReadStream('task1-upload.txt').pipe(blobService.createBlob('taskcontainer', 'task1', azure.Constants.BlobConstants.BlobTypes.BLOCK));

To download the blob and write it to the file system, a similar getBlob method can be used:

var blobService = azure.createBlobService();

blobService.getBlob('taskcontainer', 'task1').pipe(fs.createWriteStream('task1-download.txt'));

To create a SAS URL you can use the getBlobUrl method. Additionally you can use the date helper functions to easily create a SAS that expires at some point relative to the current time.

var blobService = azure.createBlobService();

//create a SAS that expires in an hour
var sharedAccessPolicy = {
    AccessPolicy: {
        Expiry: azure.date.minutesFromNow(60);
    }
};

var sasUrl = blobService.getBlobUrl(containerName, blobName, sharedAccessPolicy);

Storage Queues

The createQueueIfNotExists method can be used to ensure a queue exists:

var queueService = azure.createQueueService();
queueService.createQueueIfNotExists('taskqueue', function(error){
    if(!error){
        // Queue exists
    }
});

The createMessage method can then be called to insert the message into the queue:

var queueService = azure.createQueueService();
queueService.createMessage('taskqueue', 'Hello world!', function(error){
    if(!error){
        // Message inserted
    }
});

It is then possible to call the getMessage method, process the message and then call deleteMessage inside the callback. This two-step process ensures messages don't get lost when they are removed from the queue.

var queueService = azure.createQueueService(),
    queueName = 'taskqueue';
queueService.getMessages(queueName, function(error, serverMessages){
    if(!error){
        // Process the message in less than 30 seconds, the message
        // text is available in serverMessages[0].messagetext

        queueService.deleteMessage(queueName, serverMessages[0].messageid, serverMessages[0].popreceipt, function(error){
            if(!error){
                // Message deleted
            }
        });
    }
});

Service Bus Queues

Service Bus Queues are an alternative to Storage Queues that might be useful in scenarios where more advanced messaging features are needed (larger message sizes, message ordering, single-operaiton destructive reads, scheduled delivery) using push-style delivery (using long polling).

The createQueueIfNotExists method can be used to ensure a queue exists:

var serviceBusService = azure.createServiceBusService();
serviceBusService.createQueueIfNotExists('taskqueue', function(error){
    if(!error){
        // Queue exists
    }
});

The sendQueueMessage method can then be called to insert the message into the queue:

var serviceBusService = azure.createServiceBusService();
serviceBusService.sendQueueMessage('taskqueue', 'Hello world!', function(
    if(!error){
        // Message sent
     }
});

It is then possible to call the receiveQueueMessage method to dequeue the message.

var serviceBusService = azure.createServiceBusService();
serviceBusService.receiveQueueMessage('taskqueue', function(error, serverMessage){
    if(!error){
        // Process the message
    }
});

Service Bus Topics

Service Bus topics are an abstraction on top of Service Bus Queues that make pub/sub scenarios easy to implement.

The createTopicIfNotExists method can be used to create a server-side topic:

var serviceBusService = azure.createServiceBusService();
serviceBusService.createTopicIfNotExists('taskdiscussion', function(error){
    if(!error){
        // Topic exists
    }
});

The sendTopicMessage method can be used to send a message to a topic:

var serviceBusService = azure.createServiceBusService();
serviceBusService.sendTopicMessage('taskdiscussion', 'Hello world!', function(error){
    if(!error){
        // Message sent
    }
});

A client can then create a subscription and start consuming messages by calling the createSubscription method followed by the receiveSubscriptionMessage method. Please note that any messages sent before the subscription is created will not be received.

var serviceBusService = azure.createServiceBusService(),
    topic = 'taskdiscussion',
    subscription = 'client1';

serviceBusService.createSubscription(topic, subscription, function(error1){
    if(!error1){
        // Subscription created

        serviceBusService.receiveSubscriptionMessage(topic, subscription, function(error2, serverMessage){
            if(!error2){
                // Process message
            }
        });
     }
});

Notification Hubs

Notification hubs allow you to send notifications to WNS, APNS, GCM, and MPNS receivers.

To create a notification hub, use the method createNotificationHub.

var serviceBusService = azure.createServiceBusService();

serviceBusService.createNotificationHub('hubName', function (err) {
    if (!err) {
        // Notification hub created successfully
    }
});

To send notification using native format to the notification hub use the methods of the wns, apns, gcm, mpns objects. For a full reference on WNS method templates, check http://msdn.microsoft.com/en-us/library/windows/apps/hh779725.aspx. To send template (cross-platform) notifications use the send method on the NotificationHubService class.

var notificationHubService = azure.createNotificationHubService('hubName');

// WNS notification
notificationHubService.wns.sendTileSquarePeekImageAndText01(
    null,
    {
        image1src: 'http://foobar.com/dog.jpg',
        image1alt: 'A dog',
        text1: 'This is a dog',
        text2: 'The dog is nice',
        text3: 'The dog bites',
        text4: 'Beware of dog'
    },
    function (error) {
        if (!error) {
            // message sent successfully
        }
    });

// APNS notification
notificationHubService.apns.send(
    null,
    {
        alert: 'This is my toast message for iOS!',
        expiry: expiryDate
    },
    function (error) {
        if (!error) {
            // message sent successfully
        }
    });

// GCM notification
notificationHubService.gcm.send(
    null,
    {
        data: { message: 'Here is a message' }
    },
    function (error) {
        if (!error) {
            //message send successfully
        }
    });

// MPNS notification
notificationHubService.mpns.sendToast(
    null,
    {
        text1: 'A dog',
        text2: 'This is a dog'
    },
    function (error) {
        if (!error) {
            //message send successfully
        }
    });

// template notification
notificationHubService.send(
    null,
    {
        message: 'This is my template notification',
        goesTo: 'all registrations irrespective of the platform'
    },
    function (error) {
        if (!error) {
            //message send successfully
        }
    });

To create registrations (for both native and template notifications), use the creation methods in the wns, apns, gcm, mpns. To retrieve, update and delete existing registrations, use the following methods in NotificationHubService: getRegistration, listRegistrations, listRegistrationsByTag, updateRegistration, and deleteRegistration.

Service Runtime

The Service Runtime allows you to interact with the machine environment where the current role is running. Please note that these commands will only work if your code is running in a worker role inside the Azure emulator or in the cloud.

The isAvailable method lets you determine whether the service runtime endpoint is running on the local machine. It is good practice to enclose any code that uses service runtime in the isAvailable callback.

azure.RoleEnvironment.isAvailable(function(error, available) {
    if (available) {
        // Place your calls to service runtime here
    }
});

The getConfigurationSettings method lets you obtain values from the role's .cscfg file.

azure.RoleEnvironment.getConfigurationSettings(function(error, settings) {
    if (!error) {
        // You can get the value of setting "setting1" via settings['setting1']
    }
});

The getLocalResources method lets you find the path to defined local storage resources for the current role. For example, the DiagnosticStore resource which is defined for every role provides a location for runtime diagnostics and logs.

azure.RoleEnvironment.getLocalResources(function(error, resources) {
    if(!error){
        // You can get the path to the role's diagnostics store via
        // resources['DiagnosticStore']['path']
    }
});

The getCurrentRoleInstance method lets you obtain information about endpoints defined for the current role instance:

azure.RoleEnvironment.getCurrentRoleInstance(function(error, instance) {
    if (!error && instance['endpoints']) {
        // You can get information about "endpoint1" such as its address and port via
        // instance['endpoints']['endpoint1']['address'] and instance['endpoints']['endpoint1']['port']
    }
});

The getRoles method lets you obtain information about endpoints in role instances running on other machines:

azure.RoleEnvironment.getRoles(function(error, roles) {
    if(!error){
        // You can get information about "instance1" of "role1" via roles['role1']['instance1']
    }
});

Need Help?

Learn More

Contribute

  • If you would like to become an active contributor to this project please follow the instructions provided in Windows Azure Projects Contribution Guidelines.
  • If you encounter any bugs with the library please file an issue in the Issues section of the project.