Hapi.js is fun

Jowanza Joseph 

@jowanza

Agenda

  • State of Node.js Web Frameworks
  • Why I Chose Hapi
  • Making a Web App With Hapi.js
  • The Hapi.js Ecosystem 

The Spectrum

MVC*

Less Convention

Conventions are good

Freedom helps you learn

The Hapi Philosophy

 Configuration vs. Code

Code

The Express.js Way

var express = require('express');
var Item = require('models').Item;
var app = express();
var itemRoute = express.Router();
 
itemRoute.param('itemId', function(req, res, next, id) {
  Item.findById(req.params.itemId, function(err, item) {
    req.item = item;
    next();
  });
});
 
// Create new Items
itemRoute.post('/', function(req, res, next) {
  var item = new Item(req.body);
  item.save(function(err, item) {
    res.json(item);
  });
});
 
itemRoute.route('/:itemId')
  // Get Item by Id
  .get(function(req, res, next) {
    res.json(req.item);
  })
  // Update an Item with a given Id
  .put(function(req, res, next) {
    req.item.set(req.body);
    req.item.save(function(err, item) {
      res.json(item);
    });
  })
  // Delete and Item by Id
  .delete(function(req, res, next) {
    req.item.remove(function(err) {
      res.json({});
    });
  });
 
app.use('/api/items', itemRoute);
app.listen(8080);

Problems

  • Repetition 
  • Dependency on Middleware
  • Reasoning

The Loopback Way

var loopback = require('loopback');
var app = module.exports = loopback();
 
var Item = loopback.createModel(
  'Item',
  {
    description: 'string',
    completed: 'boolean'
  }
);
 
app.model(Item);
app.use('/api', loopback.rest());
app.listen(8080);

Problems

  • Too OOP
  • Magic

Hapi: Step-by-step

Hapi Ecosystem

Lets try some

Take Aways

  • Give Hapi a try
  • Use Hapi Ecosystem tools for testing
  • Use Hapi!

Questions?

Hapi.js: My Personal Joy(i)

By Jowanza Joseph

Hapi.js: My Personal Joy(i)

Why I use Hapi.js

  • 2,590