Slide 1

Slide 1 text

intro a hapi.js @valgreens

Slide 2

Slide 2 text

“A rich framework for building web applications and services. hapi is a simple to use configuration-centric framework with built-in support for input validation, caching, authentication, and other essential facilities. hapi enables developers to focus on writing reusable application logic instead of spending time building infrastructure. The framework supports a powerful plugin architecture for pain-free and scalable extensibility.” http://spumko.github.io

Slide 3

Slide 3 text

http://twitter.com/eranhammer/status/406300408647139328

Slide 4

Slide 4 text

http://twitter.com/eranhammer/status/406283267952738304 http://gist.github.com/hueniverse/7686452

Slide 5

Slide 5 text

https://twitter.com/eranhammer/status/406249774094876672

Slide 6

Slide 6 text

$ mkdir app $ cd app/ $ npm init $ npm install hapi --save para empezar…

Slide 7

Slide 7 text

var Hapi = require(“hapi"); ! var server = new Hapi.Server(8080, “localhost"); ! server.start(function() { console.log(“Hapi server: " + server.info.uri); }); creando el servidor…

Slide 8

Slide 8 text

server.route({ path: "/", method: "GET", handler: function(request, reply) { reply("Hello, world!"); } }); routing…

Slide 9

Slide 9 text

path * ‘mi/ruta’: localhost/mi/ruta * ‘mi/{adj}/ruta’: localhost/mi/gran/ruta o localhost/mi/peor/ ruta (request.params.adj) GET, POST, PUT, DELETE, PATCH o (*) method handler * request.params, request.query, request.payload, request.headers * reply es la interfaz para devolver una respuesta al servidor * puedes enviar: String, Object/Array, Buffer, ReadableStream * reply.file(), reply.view(), reply.close() y reply.proxy()

Slide 10

Slide 10 text

server.route({ path: "/hola/{name*2}", method: "GET", handler: function(request, reply) { var names = request.params.name.split("/"); reply({ first: names[0], last: names[1], mood: request.query.mood || "neutral" }); } }); http://localhost:8080/hola/Walter/White?mood=happy {"first":"Walter","last":"White":"mood":"happy"}

Slide 11

Slide 11 text

$ npm install joi --save validación… var Joi = require(‘joi);

Slide 12

Slide 12 text

var helloConfig = { handler: function(request, reply) { var names = request.params.name.split("/"); reply({ first: names[0], last: names[1], mood: request.query.mood }); }, validate: { params: { name: Joi.string().min(8).max(100) }, query: { mood: Joi.string().valid(["neutral","happy","sad"]).default ("neutral") } } };

Slide 13

Slide 13 text

server.route({ path: "/hello/{name*2}", method: "GET", config: helloConfig });

Slide 14

Slide 14 text

$ npm install catbox-redis --save caching… var server = new Hapi.Server(8080, "localhost", { cache: { engine: "catbox-redis", options: { host: "localhost", partition: "app", password: "mypass" } } }); http://github.com/spumko/catbox/

Slide 15

Slide 15 text

server.method("getColour", function(name, next) { var colours = ["red", "blue", “yellow", "green"]; var colour = colours[Math.floor(Math.random() * colours.length)]; next(null, colour); }, { cache: { expiresIn: 30000 } }); function(request, reply) { var names = request.params.name.split("/"); server.methods.getColour(request.params.name, function(err, colour) { reply({ first: names[0], last: names[1], mood: request.query.mood, colour: colour }); }); }

Slide 16

Slide 16 text

var helloConfig = { handler: function(request, reply) { var names = request.params.name.split(“/"); var colours = ["red", "blue", “yellow", "green"]; var colour = colours[Math.floor(Math.random() * colours.length)]; reply({ first: names[0], last: names[1], mood: request.query.mood, color: colour }); }, cache: { expiresIn: 60000 } };

Slide 17

Slide 17 text

vistas y estáticos… views: { engines: { jade: "jade" }, path: "./views" } reply.view("hello", { first: names[0], last: names[1], mood: request.query.mood, colour: colour });

Slide 18

Slide 18 text

server.route({ path: "/static/{path*}", method: "GET", handler: { directory: { path: "./public", listing: false, index: false } } });

Slide 19

Slide 19 text

logging… server.log(["test"], "This is my log entry!"); server.log(["error"], "Bogus data received from cache, unable to proceed."); server.on("log", function(event, tags) { var tagsJoined = Object.keys(tags).join(); var message = event.data; console.log("Log entry [" + tagsJoined + "] (" + (message || "") + ")"); });

Slide 20

Slide 20 text

server.route({ path: "/log/{data}", method: "GET", handler: function(request, reply) { request.log(["pathData"]); reply("Logged " + request.params.data); } }); server.on("request", function(request, event, tags) { if (tags.pathData) { console.log("Logging pathData: " + request.params.data); } });

Slide 21

Slide 21 text

plugins… $ npm install lout --save server.pack.require("lout", function(err) { if (err) throw err; server.start(function() { console.log("Hapi server: ”+server.info.uri); }); });

Slide 22

Slide 22 text

$ mkdir -p plugins/example $ cd plugins/example/ $ npm init var internals = {}; ! exports.register = function(plugin, options, next) { next(); };

Slide 23

Slide 23 text

var internals = {}; exports.register = function(plugin, options, next) { plugin.route({ path: "/mi/plugin", method: "GET", handler: function(request, reply) { reply(“Hola desde un plugin"); } }); next(); }; server.pack.require(["lout", "./plugins/example"], function(err) { if (err) throw err; server.start(function() { console.log("Hapi server: "+server.info.uri); }); });

Slide 24

Slide 24 text

server.pack.require({ "lout": {}, "./plugins/example": { // objeto con opciones } }, function(err) { if (err) throw err; server.start(function() { console.log("Hapi server: "+server.info.uri); }); });

Slide 25

Slide 25 text

packs… var pack = new Hapi.Pack(); var s1 = pack.server(8080, "localhost"); s1.route({ path: "/server/{id}", method: "GET", handler: function(request, reply) { reply(request.params.id); } }); var s2 = pack.server(8081, "localhost"); pack.require(["lout", "./plugins/example"], function(err) { if (err) throw err; pack.start(function(server) { console.log("Hapi pack started."); }); });

Slide 26

Slide 26 text

composer… var manifest = { servers: [ {port: 8080}, {port: 8081} ], plugins: ["lout", "./plugins/example"] } var composer = new Hapi.Composer(manifest); ! composer.compose(function() { composer.start(function() { console.log("Servers started"); }); });

Slide 27

Slide 27 text

enlaces de interés… * Variables de entorno y hapi: http://vawks.com/blog/ 2014/03/18/hapi-confidence/ * API Reference: http://github.com/spumko/hapi/blob/master/ docs/Reference.md * Repositorios de la suite: http://github.com/spumko * Podcast en el que hablan de #nodebf y como usan node en Walmart: http://nodeup.com/fiftysix

Slide 28

Slide 28 text

gracias :)