Running hapi in Azure

Running Node in Azure may not seem the most intuitive but turns out to be quite easy. Azure has a number of options for hosting free sites/api’s which makes trying them out and hosting basic services relatively straight forward.

The Azure website has documentation to setup a node app with automated deployment through Git. The sample uses the node http server but hapi works just as well with a few modifications.

The sample doesn’t make it clear but Azure handles the host and port environment variables a bit differently than expected. Rather than the port being an integer, as expected, it’s a string containing a named pipe. Hapi can manage Windows named pipes just fine but requires a different setup than the normal host, port parameters.

The key in the below sample is to use the port as the host if it’s a named pipe.

var hapi = require('hapi');

var host = process.env.HOST || '0.0.0.0';
var port = process.env.PORT || 8081;

var server;
if (port.toString().indexOf('pipe') >= 0) {
    // Azure uses named pipes defined in the port env variable
    server = hapi.createServer(port);
}
else {
    // running locally
    server = hapi.createServer(host, port);
}

This github issue describes the problem and resolution.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s