On Github kevinold / nashjs_hapi_presentation
A rich framework for building applications and services
var Hapi = require('hapi');
// Create a server with a host and port
var server = Hapi.createServer('localhost', 8000);
// Add the route(s)
server.route({
method: 'GET',
path: '/hello',
handler: function (request, reply) {
reply('hello world');
}
});
// Start the server
server.start();
console.log(server.table()); // routing table
Sample server with a /hello endpoint from hapijs.com
var Joi = require("joi");
server.route({
method: 'GET',
path: '/hello-world',
validate: {
params: {
name: Joi.string().min(8).max(100)
},
query: {
mood: Joi.string().valid(["neutral","happy","sad"])
.default("neutral")
}
// headers and payload too!
},
handler: function (request, reply) { reply('hello world'); }
});
server.route({
method: 'GET',
vhost: 'example.com',
path: '/hello',
handler: function (request, reply) {
reply('hello world');
}
});
server.route({
method: 'GET',
path: '/hello',
config: {
handler: function (request, reply) {
reply('hello world');
},
cache: { expiresIn: 60 * 5 * 1000 } // cache-control: max-age=300, must-revalidate
}
});
// server code here
server.pack.require('lout', function() {
server.start();
});
var Hapi = require('hapi');
Hapi.error.badRequest('Invalid parameter value');
Hapi.error.unauthorized('Stale timestamp', 'Hawk', { ts: fresh, tsm: tsm });
Hapi.error.forbidden('Missing permissions');
Hapi.error.notFound('Wrong number');
server.views({
engines: {
html: 'handlebars',
jade: 'jade'
},
path: '/templates'
});
var handler = function (request, reply) {
var context = {
title: 'Views Example',
message: 'Hello, World'
};
reply.view('hello', context);
}
server.route({
method: 'GET',
path: '/favicon.ico',
handler: {
file: 'favicon.ico'
}
});
var handler = function (request, reply) {
var response = reply('success').hold();
// lookup user id, etc.
onTimeout(function () {
response.send();
}, 1000);
};
server.state('visitor', {
ttl: 250000,
encoding: 'iron',
password: 'super-secret'
});
var handler = function (request, reply) {
reply.view('home').state('visitor', { foo: true }); // object in cookie
};
var cache = server.cache('countries',
{
expiresIn: 60 * 1000, // expires in 1 min
staleIn: 45 * 1000, // stale in 45 seconds
staleTimeout: 300 // wait before checking if an item is stale
});
var Hapi = require('hapi');
var server = new Hapi.Server();
var pre1 = function (request, reply) { reply('Hello'); };
var pre2 = function (request, reply) { reply('World'); };
var pre3 = function (request, reply) {
reply(request.pre.m1 + ' ' + request.pre.m2);
};
server.route({
method: 'GET',
path: '/',
config: {
pre: [
[
// m1 and m2 executed in parallel
{ method: pre1, assign: 'm1' },
{ method: pre2, assign: 'm2' }
],
{ method: pre3, assign: 'm3' },
],
handler: function (request, reply) {
reply(request.pre.m3 + '\n');
}
}
});
var Hapi = require('hapi');
var server = new Hapi.Server();
var options = {
subscribers: {
'console': ['ops', 'request', 'log', 'error'],
'http://localhost/logs': ['log'],
'/tmp/logs/': ['request', 'log'],
'udp://127.0.0.1:9000': ['request'],
'redis://127.0.0.1:6379/listname': ['request']
}
};
server.pack.require('good', options, function (err) {
if (!err) {
// Plugin loaded successfully
}
});