Why we chose Hapi

Walmart Labs

Our Service

  • Mixed data from multiple internal services
  • Rendered search optimized landing pages
  • Various page types
  • Overloaded URL routes

Previous Implementation

  • Express based app
  • Huge app.js entry file

Why not refactor and stick with Express?

  • Middleware got in the way most times
  • Routes harder to compose
  • History of using Express

What about Restify?

  • Very similar to Express
  • View rendering story not there

Why not Hapi already?

Hapi had only just came out

So why did we choose Hapi?

Light & Flexible API

server.route({
  method: 'GET',
  path: '/hello',
  handler: function (request, reply) {
    return reply('world');
  }
});

server.route({
  method: 'GET',
  path: '/assets',
  handler: {
    directory: {
      path: Path.join(__dirname, 'assets')
    }
  }
});

First Class Proxy

server.route({
  method: 'GET',
  path: '/walmart-proxy',
  handler: {
    proxy: {
      uri: 'http://www.walmart.com'
    }
  }
});

Plugin Architecture

exports.register = function (plugin, options, next) {

  plugin.route({
    path: '/',
    method: 'GET',
    handler: function (request, reply) {
      reply('Hello world');
    }
  });

  next();
};

exports.register.attributes = {
  pkg: require('./package.json')
};

require('hapi-peel').create(module, {
  port: 8080
});

Hapi Ecosystem

Catbox

  • Useful configurations
  • Stale TTL and timeouts
  • Wrote memcached engine

Hoek

var nestedObject = {
  foo: {
    bar: {
      value: 1
    }
  },
  tacos: [
    'Fish'
  ]
};

Hoek.reach(nestedObject, 'foo.bar.value');
// => 1

Hoek.transform(nestedObject, {
  fooBar: 'foo.bar.value',
  taco: 'tacos.0'
};
// => { fooBar: 1, taco: 'Fish' }

Lab

  • Known API
  • No globals
  • Not forced to use CLI runner
  • Built in code coverage

So
Many
More

Takeaways from using Hapi

  • Only use what you actually need
  • Keep application logic modular
  • Upgraded from 1.20.0 to 7.0.0

Jacob Chapel @jacobchapel

Why we chose Hapi

By Jacob Chapel

Why we chose Hapi

  • 951