10 minutes of

Hapiness

Daniela Borges

10 minutes of hapiness

A rich framework for building applications and services

// daniela borges       

10 minutes of hapiness

walmart case study

// daniela borges       

10 minutes of hapiness

walmart case study

// daniela borges       

10 minutes of hapiness

walmart case study

express

Most of the effort focused on changing the interaction between Express and the business logic (...) 

[lack of] support for authentication, encrypted cookies, input validation, ...

Eran Hammer

(nodejs Architect at Walmart)

( source: http://hueniverse.com/2012/12/20/hapi-a-prologue/ )

// daniela borges       

Hapi

(HTTP API server)

10 minutes of hapiness

walmart case study

  1. configuration is better than code

  2. business logic must be isolated from transport layer 

 

delivering business value

// daniela borges       

10 minutes of hapiness

A rich framework for building

applications and services

focus on writing reusable application logic

Hapi

// daniela borges       

10 minutes of hapiness

Stats

+62k downloads last month

 

Current version 8.4.0

// daniela borges       

10 minutes of hapiness

How easy could it be to

start a server?

// daniela borges       

10 minutes of hapiness

    
    var Hapi = require('hapi');
    
    var server = new Hapi.Server();

    server.connection({ 
        host: 'localhost', 
        port: 8000 
    });
    
    server.start();

Create!.. a server

// daniela borges       

10 minutes of hapiness

         
    (...)
 
    server.route({
        method: 'GET',
        path:'/api/bar', 
        handler: function (request, reply) {
           reply('Nodeschool event!');
        }
    });
    
    server.start();

Add routes

// daniela borges       

10 minutes of hapiness

Create!.. routes

may be organized in plugins

focus on writing reusable

application logic

// daniela borges       

10 minutes of hapiness

Create!.. routes using plugins

    var register = function (plugin, options, next) {
    
      plugin.route({
        method: 'GET',
        path: '/api/bar',
        handler: function(request, reply){
          // do interesting stuff
        }
      });
    
      next();
    };
    
    register.attributes = {
      name : 'barApi',
      version : '0.0.1'
    }
    
    module.exports = register;
barApi.js
// daniela borges       

10 minutes of hapiness

Create!.. routes using plugins


    var Hapi = require('hapi');
    var server = new Hapi.Server();
    server.connection(...);
    
    
    // Register the API
    server.register( [ barApi, (...) ] ,
                      function (err) {});
    
    server.start();
index.js
// daniela borges       

10 minutes of hapiness

Other relevant plugins

...'cause everything is a plugin

// daniela borges       

10 minutes of hapiness

API documentation generator

// daniela borges       

10 minutes of hapiness

    server.register( {register: require('lout')} , function (err) {
        // do something crazy with err
    });
// daniela borges       

10 minutes of hapiness

Object schema description language and validator

// daniela borges       

10 minutes of hapiness

var schema = {
    name: Joi.string().required(),
    paticipantNumber:  Joi.integer().min(0).max(500), 
    website:  Joi.uri()    
}
// daniela borges       

10 minutes of hapiness

    var Joi = require('joi');
    
    server.route({
        method: 'POST',
        path:'/api/bar', 
        handler: voodooHandlerFunc,
        config:{
            validate: 
            {
    	        payload: {
                        name: Joi.string().required(),
                	paticipantNumber:  Joi.integer().min(0).max(500),					
                	website:  Joi.uri()    
        	}
            }
        }
    });
// daniela borges       

10 minutes of hapiness

// daniela borges       

authentication

10 minutes of hapiness

Cookie authentication plugin

hapi-auth-cookie

  • defines a strategy
// register the plugin
server.register(require('hapi-auth-cookie'), function (err) {
    
    // define strategy
    server.auth.strategy('session', 'cookie', {
       
        password: 'secret',
        cookie: 'sid',
        ttl: 1000 * 60 * 60 * 24, //1 day 
        redirectTo: '/login'
    });
});
// daniela borges       

10 minutes of hapiness

Cookie authentication plugin

hapi-auth-cookie

  • manages sessions
on login
        request.auth.session.set(xptoAccount);

on logout
        request.auth.session.clear();
// daniela borges       

10 minutes of hapiness

third-party login plugin

Bell

  • supports for Google, GitHub, Twitter, Facebook, Instagram, LinkedIn, ...
  • supports OAuth 1.0a and 2.0 based login services 
// daniela borges       

10 minutes of hapiness

Relevant plugins

automatic API documentationlout

validationjoi

authenticationhapi-auth-cookie, bell, ...

error handling: boom, poop

testslab

HTTP requests: wreck

database: mongodb, ...

 

views: jade, swig, handlebars

// daniela borges       

10 minutes of hapiness

How can I start?

npm install -g makemehapi

I am stuck! What can I do?

Checkout the mentorship program

 

// daniela borges       

10 minutes of hapiness

Nodeschool International Day

// daniela borges       

Saturday, May 23, 2015

registration: meetup.com/require-lx

10 minutes of hapiness

Motivational Links

// daniela borges       

10 minutes of hapiness

Resumé

Daniela Borges 

daniela.borges@ist.utl.pt

 

 sericaia                              @sericaia

 

(web) developer | geocacher | photographer | origami master 

 

// daniela borges       

Nodeschool: 10 minutes of Hapiness

By Daniela Matos de Carvalho

Nodeschool: 10 minutes of Hapiness

  • 2,406