Using the Windows Azure SDK for Node.js on Heroku
Getting the Windows Azure SDK for Noje.js running on Heroku wasn't as easy as we thought, in this article we're going to show you how we can workaround the Heroku limitation and how to create a simple application that leverages the SDK for listing blob containers.
The limitation
As you may know, to get a Node.js application running on Heroku you need to configure the application to use the Cedar stack as described here. One of the prerequisites is that your application must run on v0.4.7 but sadly the Windows Azure SDK for Noje.js npm package requires >= 0.6.4 as defined in its package.json file, so when Heroku tries to perform the deployment it fails because of this npm dependency.
We created an issue on the Github site azure-sdk-for-node and the team will be changing it soon apparentely. If that's the case, then go straight to the Creating the application section.
Workaround 1
Basically we created Windows Azure SDK for Noje.js fork, we tweaked the package.json file and use a tarball from GitHub as the npm package reference instead of using the official 0.6.5 package. Here are the steps:
- Create a new fork from GitHub of the azure-sdk-for-node repository.
Open the
package.jsonfile and update the node engine requirement from>= v0.6.5to>= 0.4.7."engines": { "node": ">= 0.4.7" }Commit / Push your changes to GitHub
git commit -a -m "# downgrading node engine dependency"
git pushThen you can define the dependency in the package.json of your project using the
tarballgenerated by GitHub as we will see further in the sample application."azure": "https://github.com/jpgarcia/azure-sdk-for-node/tarball/master"
NOTE: If you don't want to perform any other modification to the code, you can just use the tarball from my fork. If you want to use your fork replace jpgarcia by your Github's username.
DISCLAIMER: We didn't test all the library's functionality but it seems to be working fine running on v0.4.7.
Workaround 2
There is a Running Your Own Node.js Version on Heroku blog post that describes how to deploy a custom version of Node.js. We didn't use this approach but it should work.
Creating the application
We'll use the express npm package to create the application, so lets start.
Create a new
package.jsonfile and configure theexpress,ejsandazure(using the tarball) as the project dependencies:{
"name": "heroku-azure-storage-sample" ,
"version": "0.0.1" ,
"dependencies": {
"express": "2.5.2",
"ejs": "0.6.1",
"azure": "https://github.com/jpgarcia/azure-sdk-for-node/tarball/master"
}
}Create a
web.jsfile with the following application logic to retrieve the containers for a given account:var express = require('express')
, azure = require('azure');
/*
* Configuration
* ---------------------------------------- */
var app = express.createServer(express.logger());
app.register('html', require('ejs'));
app.set('view engine', 'html');
/*
* Routes
* ---------------------------------------- */
app.get('/', function (req, res) {
var blobClient = azure.createBlobService(process.env['WAZ_STORAGE_ACCOUNT_NAME'], process.env['WAZ_STORAGE_ACCESS_KEY'])
.withFilter(new azure.ExponentialRetryPolicyFilter());
blobClient.listContainers({}, function(err, result) {
res.render('index', { layout: false, containers: result });
});
});
/*
* Bootstrap
* ---------------------------------------- */
var port = process.env.PORT || 3000;
app.listen(port, function () {
console.log("Listening on " + port);
});Configure your account
WAZ_STORAGE_ACCOUNT_NAMEandWAZ_STORAGE_ACCOUNT_ACCESSS_KEYenvironment variables:If you are running Node.js on Windows, from the command line:
SET WAZ_STORAGE_ACCOUNT_NAME=accountname
SET WAZ_STORAGE_ACCESS_KEY=accountkeyIf you are running Node.js on MacOS, from a console:
export WAZ_STORAGE_ACCOUNT_NAME=accountname
export WAZ_STORAGE_ACCESS_KEY=accountkey
Create a new
viewsfolder with anindex.htmlfile inside with to display the list of containers retrieved from Azure:<!doctype html>
<html lang="en">
<head>
<title>WAZ Storage Sample</title>
</head>
<body>
<h1>Containers</h1>
<ul>
<% for(var i=0;i<containers.length;i++) { %>
<li><%= containers[i].name %></li>
<% } %>
</ul>
</body>
</html>Intall the npm dependencies, from the command line:
npm installRun application locally:
node web.jsBrowse the application:
http://localhost:3000
Deploying the Applicattion to Heroku
To deploy the application you must have installed the Heroku Toolbelt.
Create a Procfile in your project's root folder to declare the commands to run when the application is deployed to Heroku. In this case the
Procfilewill have only one row for running the website:web: node web.jsCreate a
.gitignorefile in your project's root file with the following content:node_modulesInitialize a new git repository:
git init
git add .
git commit -m "init"Create a new application on Heroku specifying the cedar stack:
heroku create your-application-name --stack cedarSet the environment variables with your Azure account name and key:
heroku config:add WAZ_STORAGE_ACCOUNT_NAME=accountname
heroku config:add WAZ_STORAGE_ACCESS_KEY=accountkeyDeploy your application
git push heroku masterThat's it, now you can navigate your application at:
http://your-application-name.herokuapp.com
View the discussion thread.blog comments powered byDisqus