An introduction to hapi.js
Dave Stevens
@davestevens84 | davestevens84@gmail.com
What is hapi.js?
• Framework for building web applications and
services
• Eran Hammer
• Walmart
• Robust, Stable, Reliable
• Security is a key consideration
BENEFITS
What’s so great about hapi
Modularization
• Allows for logical modularization of your
application
• Rich plugin interface
• Large application can be easily distributed
over multiple smaller modules
Tested at scale
• All Walmart Black Friday traffic 2 years
running
• Didn’t explode
• APIs and web applications at Yahoo, Disney,
Paypal and Zappos
Eco-system
• Good (process monitoring and logging)
• Good-replay (replay log events!)
• Glue (compose servers from manifests)
• TV (Interactive debug console)
• Joi (Schema validation)
• Etc… https://github.com/hapijs
Installing
• npm install --save hapi
Creating a server
var Hapi = require(‘hapi’);
var server = new Hapi.Server();
server.connection({
host:’localhost’,
port: 9009
});
Creating a server (2)
server.route({
path: ‘/’,
method: ‘GET’,
handler: function (request, reply) {
reply(‘Hi VegasJS!’);
}
});
server.start();
Routing
server.route({
path: ‘/’,
method: ‘GET’,
handler: function (request, reply) {
reply(‘Hi VegasJS!’);
}
});
Values surrounded by {} populate request.params in handler
e.g. path:‘/name/{firstname}’ would mean that /name/dave results
in request.params.firstname being equal to ‘dave’
Very configurable to handle multitude of eventualities – one example,
multi-segment parameters:
path: ‘/name/{names*}’ then requesting /name/dave/andrew/stevens
would result in request.params.names being ‘dave/andrew/stevens’
which we can split on ‘/’ to create array, etc.
Routing
server.route({
path: ‘/’,
method: ‘GET’,
handler: function (request, reply) {
reply(‘Hi VegasJS!’);
}
});
Specify HTTP method here, can also have multiple methods in
An array eg.
method: [‘PUT’, ‘POST’],
Routing
server.route({
path: ‘/’,
method: ‘GET’,
handler: function (request, reply) {
reply(‘Hi VegasJS!’);
}
});
Plugins
• Allow you to build out your application
components as self-contained modules
• Also manipulate requests and other events,
hook in to server events, etc.
• Kinda like middleware but think bigger!
Decorate
• Avoid duplicate code by using
server.decorate
server.decorate(‘reply’, ‘error’, function (err) {
console.error(err);
return this.response({message: ‘There was an
error’}).code(500);
});
// in handler, reply.error(err);
Next Steps
• > npm install –g makemehapi
• > makemehapi
• Challenges and guided learning!
• Talk to me
• #hapijs on Twitter (official: @hapijs)
• #hapi on Freenode IRC
• http://bit.ly/orghapi
Obligatory “Look, I Finished” Slide
• Twitter @davestevens84
• Email davestevens84@gmail.com
• Accost me in the street

An Introduction to hapi.js

  • 1.
    An introduction tohapi.js Dave Stevens @davestevens84 | davestevens84@gmail.com
  • 2.
    What is hapi.js? •Framework for building web applications and services • Eran Hammer • Walmart • Robust, Stable, Reliable • Security is a key consideration
  • 3.
  • 4.
    Modularization • Allows forlogical modularization of your application • Rich plugin interface • Large application can be easily distributed over multiple smaller modules
  • 5.
    Tested at scale •All Walmart Black Friday traffic 2 years running • Didn’t explode • APIs and web applications at Yahoo, Disney, Paypal and Zappos
  • 6.
    Eco-system • Good (processmonitoring and logging) • Good-replay (replay log events!) • Glue (compose servers from manifests) • TV (Interactive debug console) • Joi (Schema validation) • Etc… https://github.com/hapijs
  • 7.
  • 8.
    Creating a server varHapi = require(‘hapi’); var server = new Hapi.Server(); server.connection({ host:’localhost’, port: 9009 });
  • 9.
    Creating a server(2) server.route({ path: ‘/’, method: ‘GET’, handler: function (request, reply) { reply(‘Hi VegasJS!’); } }); server.start();
  • 10.
    Routing server.route({ path: ‘/’, method: ‘GET’, handler:function (request, reply) { reply(‘Hi VegasJS!’); } }); Values surrounded by {} populate request.params in handler e.g. path:‘/name/{firstname}’ would mean that /name/dave results in request.params.firstname being equal to ‘dave’ Very configurable to handle multitude of eventualities – one example, multi-segment parameters: path: ‘/name/{names*}’ then requesting /name/dave/andrew/stevens would result in request.params.names being ‘dave/andrew/stevens’ which we can split on ‘/’ to create array, etc.
  • 11.
    Routing server.route({ path: ‘/’, method: ‘GET’, handler:function (request, reply) { reply(‘Hi VegasJS!’); } }); Specify HTTP method here, can also have multiple methods in An array eg. method: [‘PUT’, ‘POST’],
  • 12.
    Routing server.route({ path: ‘/’, method: ‘GET’, handler:function (request, reply) { reply(‘Hi VegasJS!’); } });
  • 13.
    Plugins • Allow youto build out your application components as self-contained modules • Also manipulate requests and other events, hook in to server events, etc. • Kinda like middleware but think bigger!
  • 14.
    Decorate • Avoid duplicatecode by using server.decorate server.decorate(‘reply’, ‘error’, function (err) { console.error(err); return this.response({message: ‘There was an error’}).code(500); }); // in handler, reply.error(err);
  • 15.
    Next Steps • >npm install –g makemehapi • > makemehapi • Challenges and guided learning! • Talk to me • #hapijs on Twitter (official: @hapijs) • #hapi on Freenode IRC • http://bit.ly/orghapi
  • 16.
    Obligatory “Look, IFinished” Slide • Twitter @davestevens84 • Email davestevens84@gmail.com • Accost me in the street