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.

Comparison nodejs frameworks using Polls API

724 views

Published on

3 NodeJS implementations of Polls API in KoaJS, ExpressJS and Hapi.

All implementations are tested by Dredd.

Published in: Technology
  • Login to see the comments

Comparison nodejs frameworks using Polls API

  1. 1. Comparison NodeJS frameworks ladislav@prskavec.net @abtris www.praguejs.cz @jsconfcz
  2. 2. express koa hapi github stars 18861 6174 4228 contributors 178 59 116 downloads / w 525941 8769 11966 stack overflow 14853 138 66 npm -ls | wc -l 48 36 48 file size 3896 2000 50392
  3. 3. http://docs.nodeschoolhk.apiary.io/
  4. 4. https://github.com/apiaryio/dredd
  5. 5. Init server
  6. 6. "express": "^4.12.3" (function() { 'use strict'; var express = require('express'); var app = express(); app.set('json spaces', 2); .... var server = app.listen(3003, function() { var host = server.address().address; var port = server.address().port; console.log('Example app listening at http://%s:%s', host, port); }); }());
  7. 7. "hapi": "^8.4.0" var Hapi = require('hapi'); var server = new Hapi.Server(); server.connection({port: 3001}); .... // Start the server server.start(function() { console.log('Server running at:', server.info.uri); });
  8. 8. "koa": "^0.20.0" "use strict"; var koa = require('koa'); var route = require('koa-route'); var app = koa(); require('koa-qs')(app); .... app.listen(3000);
  9. 9. Routing
  10. 10. "express": "^4.12.3" app.get('/questions/:question_id', function(req, res) { res.status(200).json({"question":"Favourite programming language?"...}]}); }); app.post('/questions/:question_id/choices/:choice_id', function(req, res) { res.set('Location', '/questions/' + req.params.question_id); res.status(201).send(''); });
  11. 11. "hapi": "^8.4.0" server.route({ method: 'GET', path: '/questions/{question_id}', handler: function(request, reply) { reply({"question":"Favourite programming language?"...}); } }); server.route({ method: 'POST', path: '/questions/{question_id}/choices/{choice_id}', handler: function(request, reply) { reply().code(201).header('Location', '/questions/' + request.params.question_id); } });
  12. 12. "koa": "^0.20.0" app.use(route.get('/questions/:question_id', function *(question_id) { this.body = {"question":"Favourite programming language?"...}; })); app.use(route.post('/questions/:question_id/choices/:choice_id', function *(question_id, choice_id) { this.status = 201; this.set('Location', '/questions/' + question_id); this.body = ''; }));
  13. 13. Testing • Dredd var hooks = require('hooks'); var before = hooks.before; hooks.beforeEach(function(transaction) { if (transaction.expected.headers['Content-Type'] == 'application/json') { transaction.expected.headers['Content-Type'] = 'application/json; charset=utf-8'; } });
  14. 14. Github repository of project: https://github.com/abtris/nodeschool-hk-2015-05-23 Ideas how project improve: • you can fork and contribute • you can try add 404, 500 pages • you can try persistent layer using Redis

×