Upgrade to Pro — share decks privately, control downloads, hide ads and more …

hapi.js - An introduction

hapi.js - An introduction

Slides (and link to demo code) of my talk at TorinoJS (2016-10-26)

Carlo Perassi

October 26, 2016
Tweet

More Decks by Carlo Perassi

Other Decks in Programming

Transcript

  1. INTRODUCTION Thanks to Giorgio Cerruti and Marco Becchio : three

    services in production ATM ★ Are you a freelance? Feel free to contact me ☻ Kiwifarm Srl
  2. HAPI.JS: A FEW NOTES (1/2) ECMAScript 2015 Node.js framework (not

    Express based!) mostly used for creating REST services Robust, stable, reliable and not (so) opinionated ( ) Security is a key consideration @WalmartLabs
  3. ( website) PLUGIN BASED “hapi enables developers to focus on

    writing reusable application logic instead of spending time building infrastructure” hapi.js
  4. A VERY BASIC PLUGIN exports.register = function (server, options, next)

    { const sayHello = function (name = 'world') { return `Hello ${name}`; }; server.expose({ sayHello: sayHello, }); next(); }; exports.register.attributes = { name: 'greeting' }; // usage: server.plugins.greeting.sayHello('Carlo')
  5. ECOSYSTEM To name a few: (HTTP-friendly error objects) (process monitoring

    and logging) (guess it) (guess it once again) (object schema validation) boom good hapi-swagger hapi-auth-jwt2-swagger joi The whole pack at hapijs.com/plugins
  6. DEMO (1) # GET /api/v1/users/token_free http localhost:1337/api/v1/users/token_free -j --verbose #

    show the code # POST /api/v1/users/login http POST localhost:1337/api/v1/users/login \ [email protected] password=qqq -j --verbose # {success, failure} then show the code
  7. DEMO (2) # GET /api/v1/users/restricted http --auth-type=jwt --auth='{token}:' -j --verbose

    \ localhost:1337/api/v1/users/restricted # {success, failure} then show the code console.log(require('crypto').randomBytes(256).toString('base64'))
  8. DEMO (3) # GET /documentation # (swagger's web interface) #

    GET /api/v1/users/foo/{imei} # AMQP and folders # hidden demo :)
  9. DEMO (4) # Linting ./node_modules/lab/bin/lab --lint --lint-fix lib/ # Testing

    and code coverage ./node_modules/lab/bin/lab -C -c -v test/
  10. FURTHER STUDIES - ★☆☆ JWT-ready starter project based on hapi.js

    and (promised SQL query builder) hapi-knex-starter Knex.js
  11. FURTHER STUDIES - ★★☆ (3/5) server.route({ method: 'GET', path: '/',

    config: { pre: [ [ { method: pre1, assign: 'm1' }, { method: pre2, assign: 'm2' } ], { method: pre3, assign: 'm3' }, ], handler: function (request, reply) { return reply(request.pre.m3 + '\n'); } } }); See S.O.
  12. FURTHER STUDIES - ★★☆ (4/5) server.route({ method: 'GET', path: '/admin',

    handler: function(request, reply) {}, config: { plugins: { policies: [ ['isA', 'isB'], /* 2 in || */ 'onlyC' ] } } }); ( ) MrHorse
  13. FURTHER STUDIES - ★★☆ (5/5) { method: 'DELETE', path: '/cars/{id}',

    config: { handler: function(request, reply) { reply('car deleted!'); }, plugins: { hapiRouteAcl: { permissions: ['cars:delete'] } } } }, ( ) Hapi Route ACL
  14. FURTHER STUDIES - ★★★ (3/4) @controller('/hello') class TestController { constructor(target)

    { this.target = target } @get('/world') sayHello(request, reply) { reply({ message: `hello, ${this.target}` }) } } hapi-decorators