Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Building an API in Node with HapiJS

3,991 views

Published on

An introductory look at building an API using NodeJS and HapiJS

Published in: Technology
  • Be the first to comment

Building an API in Node with HapiJS

  1. 1. Building an API in Node with HapiJS SoCal Code Camp . Cal State Fullerton . March 7, 2015 Loc Nguyen
  2. 2. require('selfie'); • Ruby, JavaScript, C#, Java….PHP. New to Node! • API & microservice planning, UI architecture • locnguyen.com • @locn
  3. 3. AngularJS-OC http://meetup.com/AngularJS-OC
  4. 4. Agenda • What is HapiJS • Creating an API server • Configuring routes • Requesting handling • Data validation • Plug-ins • Securing the API • Server side caching • Client side caching
  5. 5. What is HapiJS? • Yet another NodeJS web framework • Not as much mindshare as Express • Built with developer scalability in mind • Built by Walmart Labs and Eran Hamme • Configuration over code
  6. 6. The server 1. npm init 2. npm install --save hapi nodemon
  7. 7. The server var Hapi = require('hapi'); var server = new Hapi.Server(); server.connection({ port: 9000 }); server.start(function () { console.log('Server available at %s', server.info.uri); });
  8. 8. The server > nodemon index.js Server available at http://localhost:9000
  9. 9. Cool Server things var Hapi = require('hapi'); var server = new Hapi.Server(); var c = server.connection({ port: 9000, labels: ['core'] }); var a = server.connection({ port: 3000, labels: ['admin'] }); server.start(function () { console.log('Core available at %s’, c.info.uri); console.log('Admin available at %s’, a.info.uri); });
  10. 10. Configuring routes server.route({ path: '/product/{id}', method: 'GET', handler: function (request, reply) { reply('Sup dawg'); } });
  11. 11. Request handling server.route({ path: '/product/{id}', method: 'GET', handler: function (request, reply) { var prodId = request.params.id; console.log('Product id: %s', prodId); console.log('Include reviews: %s', request.query.includeReviews); reply({ id: prodId }); } }); • Demo
  12. 12. Request handling server.route({ path: '/login', method: 'POST', handler: function (request, reply) { var json = request.payload; console.log(json.email, json.password); reply('Nope!').status(401); } }); • Demo
  13. 13. Data Validation • Done declaratively with object schema definition • Joi framework – https://github.com/hapijs/joi • Simple value validations • Regex • Array of valid values • Conditional • Demo
  14. 14. Plugins • Plugins allow complete isolation of logic • Configurable per labeled server • Just register on the server server.register([{ register: require('./fooPlugin'), options: { bar: 'hello' } }]); • Demo
  15. 15. Securing the API • It depends...everyone’s scheme is different • Accomplished via plugins • Flexible, can be configured per route • See http://hapijs.com/tutorials/auth server.register(require('hapi-auth-jwt'), function (err) { server.auth.strategy('token', 'jwt', 'required', { key: server.settings.app.jwtSecret }); });
  16. 16. Server side caching • Comes with in-memory cache for local development and testing • Supports a variety of solutions 1. memcached 2. Mongodb 3. Riak 4. Redis
  17. 17. Server side caching • In-memory is the default • Can configure multiple types of caches • Hapi talks to caches via Catbox abstraction • Caching services implement strategy, e.g. catbox- redis • Set up caches when creating a Hapi server • Example and demo
  18. 18. Server side caching var server = new Hapi.Server({ cache: [{ name: 'fooRedisCache' engine: require('catbox-redis'), host: '127.0.0.1', port: 6379 partition: 'cache' }] });
  19. 19. Client side caching • Support built in for HTTP caching headers • Cache-Control • Last-Modified • E-Tag • Examples
  20. 20. Client side caching server.route({ … config: { cache: { expiresIn: 1000 * 60 * 60 // 1 hour } } });
  21. 21. Client side caching var doc = { … }; reply(doc).header('Last-Modified', new Date().toUTCString()); reply(doc).etag(docChecksum); • Demo
  22. 22. fin ● Hapi makes it easy to not build a monolith ● Simple to separate business logic from web plumbing ● Plugin architecture makes it easy to swap in changes (or add more)
  23. 23. Thanks! Questions? lochnguyen@gmail.com, @locn, locnguyen.com Further topics: ● Logging ● Testing ● Proxies ● View engines ● Request lifecycle

×