Why people are chossing Hapi



Express 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' }many more ..




method: 'PUT',
path: '/api/v1.1/employee/{id}',
config: {
handler: ctrl.employee.updateById,
description: 'Updates a Employee',
tags: ['api', 'employee'],
validate: {
params: {
id: Joi.string().alphanum().required()
},
payload: {
firstName: Joi.string().trim().min(3).max(100),
lastName: Joi.string().trim().min(3).max(100),
email: Joi.string().email().trim(),
track: Joi.string().trim().min(3).max(50),
projects: Joi.array().includes
(Joi.object().pattern(/[[:alnum:]]{24}/, Joi.string()))
}
}, module.exports = {
create: function(request, reply){
var company = request.payload;
Company.create(company, function(err, newComapny){
if(err){
return reply(Boom.badImplementation(err));
} else {
var companyObj = newComapny.toObject();
return reply(companyObj).code(201);
}
});
},So
Many
More
hapi was created around the idea that configuration is better than code, thatbusiness logic must be isolated from the transport layer...
Hapi is plugin based API development
Hapi js Training 5.0
By Tarun Sharma
Hapi js Training 5.0
- 877