This document introduces the hapi.js framework for building web applications and services. It was created by Eran Hammer and is used by companies like Walmart. Hapi.js focuses on modularity, reliability, and security. It allows for logical modularization of applications into smaller modules. It has been tested at large scale without issues, such as all of Walmart's Black Friday traffic for two years. The document discusses how to install hapi.js, create a basic server, add routes and handlers, use plugins, and decorate the server. It recommends next steps like using the makemehapi tool for guided learning and provides contact details for the author.
1. An introduction to hapi.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
4. Modularization
• Allows for logical 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 (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
8. Creating a server
var Hapi = 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.
13. 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!
14. 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);
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, I Finished” Slide
• Twitter @davestevens84
• Email davestevens84@gmail.com
• Accost me in the street