Daniela Matos de Carvalho
Software Engineer @Dashlane, mother, photographer amateur, former @requirelx organiser, prev @YLDio, @zpx_interactive
Daniela Borges
Opensoft Barcamp - April 25, 2015
// daniela borges
// daniela borges
// daniela borges
Most of the effort focused on changing the interaction between Express and the business logic (...)
[lack of] support for authentication, encrypted cookies, input validation, ...
Eran Hammer
(nodejs Architect at Walmart)
( source: http://hueniverse.com/2012/12/20/hapi-a-prologue/ )
// daniela borges
(HTTP API server)
// daniela borges
// daniela borges
// daniela borges
// daniela borges
var Hapi = require('hapi');
var server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 8000
});
server.start();
// daniela borges
(...)
server.route({
method: 'GET',
path:'/api/barcamp',
handler: function (request, reply) {
reply('Opensoft barcamp event!');
}
});
server.start();
// daniela borges
// daniela borges
var register = function (plugin, options, next) {
plugin.route({
method: 'GET',
path: '/api/barcamp',
handler: function(request, reply){
// do interesting stuff
}
});
next();
};
register.attributes = {
name : 'barcampApi',
version : '0.0.1'
}
module.exports = register;
barcampApi.js
// daniela borges
var Hapi = require('hapi');
var server = new Hapi.Server();
server.connection(...);
// Register the API
server.register( [ barcampApi, (...) ] ,
function (err) {});
server.start();
index.js
// daniela borges
// daniela borges
// daniela borges
server.register( {register: require('lout')} , function (err) {
// do something crazy with err
});
// daniela borges
// daniela borges
var schema = {
name: Joi.string().required(),
paticipantNumber: Joi.integer().min(0).max(500),
website: Joi.uri()
}
// daniela borges
var Joi = require('joi');
server.route({
method: 'POST',
path:'/api/barcamp',
handler: voodooHandlerFunc,
config:{
validate:
{
payload: {
name: Joi.string().required(),
paticipantNumber: Joi.integer().min(0).max(500),
website: Joi.uri()
}
}
}
});
// daniela borges
// daniela borges
// register the plugin
server.register(require('hapi-auth-cookie'), function (err) {
// define strategy
server.auth.strategy('session', 'cookie', {
password: 'secret',
cookie: 'sid',
ttl: 1000 * 60 * 60 * 24, //1 day
redirectTo: '/login'
});
});
// daniela borges
on login
request.auth.session.set(xptoAccount);
on logout
request.auth.session.clear();
// daniela borges
// daniela borges
// daniela borges
// daniela borges
// daniela borges
Saturday, May 23, 2015
registration: meetup.com/require-lx
// daniela borges
// daniela borges
By Daniela Matos de Carvalho
Software Engineer @Dashlane, mother, photographer amateur, former @requirelx organiser, prev @YLDio, @zpx_interactive