As they say on their website
The following slides include Hapi mechanics you should consider first before reading code samples.
For progressive understanding of the concepts and samples, please prefer reading this up to date documentation instead of the official site guides.
We'll skip on purpose the views and rendering part since we only care about building bridges between endpoints and services.
Reply API is confusing at first. The reply function understands values, buffers, streams, promises.
const Hapi = require('hapi');
const server = new Hapi.Server();const Hapi = require('hapi');
const CatboxRedis = require('catbox-redis');
const server = new Hapi.Server({
cache: CatboxRedis,
connections: {
load: {
maxHeapUsedBytes: 2e8, // bytes
maxEventLoopDelay: 20 // ms
}
},
useDomains: false
});with some options
The simplest instantiation
const abcServer = server.connection({
port: 3000,
labels: ['a', 'b', 'c']
});const abcServer = server.select(['a']);Retrievable by label
const HapiAuthBasicPlugin = require('hapi-auth-basic');
server.register(HapiAuthBasicPlugin); // register 'basic' scheme
server.auth.strategy('simple', 'basic', {
validateFunc: (request, username, password, callback) => {
callback(null, true, someUser);
}
});
server.route({ method: 'GET', path: '/', config: { auth: 'simple' } });Basic
server.auth.scheme('custom', (server, options) => ({
authenticate: (request, reply) => {
reply.continue({ credentials: someUser });
}
}));
server.auth.strategy('default', 'custom');Custom
server.route({
method: 'GET',
path: '/user/{userId}',
config: {
auth: 'default',
cache: { expiresIn: 5000 },
ext: {
onPreHandler: (request, reply) => { reply.continue(); }
},
validate: {
params: {
userId: Joi.string().required()
}
},
handler: (request, reply) => reply(fetchUser(request.params.userId)
}
});Warning: no nested route system included
const schema = Joi.object().keys({
username: Joi.string().alphanum().min(3).max(30).required(),
password: Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/),
access_token: [Joi.string(), Joi.number()],
birthyear: Joi.number().integer().min(1900).max(2013),
email: Joi.string().email()
}).with('username', 'birthyear').without('password', 'access_token');Hapi has several places you can provide a Joi schema (at least an object with a validate function)
Here is a sample
But the best is to visit their documentation
server.route({
method: 'GET',
path: '/',
handler: (request, reply) => reply(42)
});
server.inject('/').should.become(42);start a simple server with a single route
request it with a postman (or what you prefer)
instrument it to get the time spent in the handlers
Of course, keep helping yourself with the documentation: https://github.com/hapijs/hapi/blob/master/API.md