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

Node.js: from express to koa

Node.js: from express to koa

This session will show you a few http frameworks, mainly from express to koa. How they work, a few examples with source code, my personal experiences with pros and cons to maybe make your decision a bit easier.

Robert Prediger

April 21, 2016
Tweet

More Decks by Robert Prediger

Other Decks in Programming

Transcript

  1. Page 1 Node.js From express to koa: http frameworks MNUG,

    April 2016, München 22-Apr-16 Node.js – from the Beginning
  2. Page 2 §  Robert Prediger §  20 years: Progress § 

    15 years: Web development §  5 years: Node.js §  [email protected] 22-Apr-16 Introduction Node.js – from the Beginning
  3. Page 3 Fast, unopinionated, minimalist web framework for Node.js Minimal

    and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. http://expressjs.com/ Node.js – from the Beginning express 22-Apr-16
  4. Page 4 express 22-Apr-16 Node.js – from the Beginning favicon

    Static-Files Log Process Request ... (other middleware modules)
  5. Page 5 var express = require('express'), app = express(); app.use(

    function( req, res, next ) { var start = new Date; console.log( "way down", req.url ); next(); console.log( 'way back' ); console.log( 'Response-Time', new Date - start ); }); app.get( "/", function( req, res ) { console.log( "req" ); res.send( "Hello World" ); }); app.listen( 80 ); Node.js – from the Beginning express 22-Apr-16
  6. Page 6 var express = require('express'), app = express(); app.use(

    function( req, res, next ) { var start = new Date; console.log( "way down", req.url ); next(); console.log( 'way back' ); console.log( 'Response-Time', new Date - start ); }); app.get( "/", function( req, res ) { process.nextTick( function() { console.log( "req" ); res.send( "Hello World" ); }); }); app.listen( 80 ); Node.js – from the Beginning express 22-Apr-16
  7. Page 7 var express = require('express'), app = express(); app.get(

    "/rest/:table", auth, function( req, res ) { res.send( "Request: " + req.params.table ); }); app.get( "/rest/:table/:id", auth, function( req, res ) { res.send( req.params ); }); app.get( "/", function( req, res ) { console.log( "req" ); res.send( "Hello World" ); }); app.listen( 80 ); Node.js – from the Beginning express 22-Apr-16
  8. Page 8 ... function auth( req, res, next ) {

    // Database request db.collection.find( “user“, { user: req.session.userid }, function( err, data ) { // no user found if (err || !data) { console.log( “Not a valid user!“ ); return res.status( 500 ).send( “Not a valid user!“ ); } // valid user -> go down the chain console.log( “Valid user!“ ); next(); }); } Node.js – from the Beginning express 22-Apr-16
  9. Page 9 ... // auth function auth( req, res, next

    ) { // check for user if (!req.query.user) { console.log( "Not a valid user" ); res.status( 500 ).send( "Not a valid user" ); return; } // valid user console.log( "Valid user" ); next(); } Node.js – from the Beginning express 22-Apr-16
  10. Page 10 var express = require('express'), app = express() ,

    router = express.Router(); // use router with prefix app.use( "/rest", auth, router ); router.get( "/:table", function( req, res ) { res.send( "Request: " + req.params.table ); }); router.get( "/:table/:id", function( req, res ) { res.send( req.params ); }); app.get( "/", function( req, res ) { console.log( "req" ); res.send( "Hello World" ); }); app.listen( 80 ); Node.js – from the Beginning express 22-Apr-16
  11. Page 11 Enterprise Node to Power the API Economy No

    matter where you are in the lifecycle of developing APIs with Node, StrongLoop has a complete set of command-line and graphical tools that can work together or independently to help you succeed. https://strongloop.com/ Node.js – from the Beginning StrongLoop 22-Apr-16
  12. Page 15 The web framework of your dreams Built for

    developers by a giant squid. Sails makes it easy to build custom, enterprise-grade Node.js apps. It is designed to emulate the familiar MVC pattern of frameworks like Ruby on Rails, but with support for the requirements of modern apps. http://sailsjs.org/ Node.js – from the Beginning SailsJS 22-Apr-16
  13. Page 16 SailsJS Node.js – from the Beginning 22-Apr-16 Source:

    https://www.manning.com/books/sails-js-in-action
  14. Page 17 SailsJS Node.js – from the Beginning 22-Apr-16 Source:

    https://www.manning.com/books/sails-js-in-action
  15. Page 18 next generation web framework for node.js Koa is

    a new web framework designed by the team behind Express, which aims to be a smaller, more expressive, and more robust foundation for web applications and APIs. http://koajs.com/ Node.js – from the Beginning Koa 22-Apr-16
  16. Page 19 var koa = require('koa'), app = koa(); app.use(

    function *(next){ var start = new Date; yield next; var ms = new Date - start; this.set('Response-Time', ms + 'ms'); }); app.use( function *(){ this.body = 'Hello World'; }); app.listen( 80 ); Node.js – from the Beginning Koa (1.x) 22-Apr-16
  17. Page 20 var Koa = require('koa'), app = new Koa(),

    co = require('co'); app.use( co.wrap( function *( ctx, next){ const start = new Date; yield next(); var ms = new Date - start; console.log( 'Response-Time', ms ); })); app.use( ctx => { ctx.body = 'Hello World'; }); app.listen( 80 ); Node.js – from the Beginning Koa (2.x) 22-Apr-16
  18. Page 21 npm install koa-router /opt/dev/test └── [email protected] npm install

    koa-router@next /opt/dev/test └── [email protected] Node.js – from the Beginning Koa – middleware modules 22-Apr-16
  19. Page 22 const Koa = require('koa'), app = new Koa(),

    co = require('co'), Router = require('koa-router'), router = new Router({ prefix: "/rest" }); router.get( "/:table", function( ctx ) { ctx.body = "Request: " + ctx.params.table; }); router.get( "/:table/:id", function( ctx ) { ctx.body = ctx.params; }); app.use( router.routes() ); app.use( ctx => { ctx.body = 'Hello World'; console.log( 'req' ); }); app.listen( 80 ); Node.js – from the Beginning Koa - Router 22-Apr-16
  20. Page 23 The Web framework beyond your dreams ThinkJS is

    the first Node.js MVC framework that supporting use full ES6/7 features to develop Node.js application. https://thinkjs.org/ Node.js – from the Beginning ThinkJS 22-Apr-16
  21. Page 27 Disadvantages Even though ThinkJS has many advantages, it

    has also a few disadvantages too, for example: •  ThinkJS is a relatively new framework, the community is not strong enough. •  ThinkJS is short of large scale applications. Node.js – from the Beginning ThinkJS 22-Apr-16
  22. Page 28 Build powerful back-end with no effort. Fast. Reusable.

    Easy to use. The next generation framework for Node.js built on top of Koa. http://strapi.io/ Node.js – from the Beginning strapi 22-Apr-16
  23. Page 32 22-Apr-16 Node.js – from the Beginning That‘s it

    J Thank you very much for listening [email protected] Sourcecode:https://github.com/web4biz/Node.js-FromTheBeginning