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

koa introduction (Node.js Web Framework)

A33cfc536667317a0cc6751c01269153?s=47 andre
October 23, 2014

koa introduction (Node.js Web Framework)

koa is a Node.js Web Framework. I've demonstrated some examples and also introduced some basic concepts in this internal sharing.

A33cfc536667317a0cc6751c01269153?s=128

andre

October 23, 2014
Tweet

Transcript

  1. Andre Lee next generation web framework for node.js koa

  2. agenda • What is koa • Before Diving in koa

    • Hello koa • Running koa in Production?
  3. agenda • What is koa • Before Diving in koa

    • Hello koa • Running koa in Production?
  4. Node.js Web Frameworks

  5. Node.js Web Frameworks • Express • Minimal, Muture, Ecosystem

  6. Node.js Web Frameworks • Express • Minimal, Muture, Ecosystem

  7. Node.js Web Frameworks • Express • Minimal, Muture, Ecosystem •

    hapi • Rich Framework
  8. Node.js Web Frameworks • Express • Minimal, Muture, Ecosystem •

    hapi • Rich Framework
  9. Node.js Web Frameworks • Express • Minimal, Muture, Ecosystem •

    hapi • Rich Framework • LoopBack • For creating APIs
  10. Node.js Web Frameworks • Express • Minimal, Muture, Ecosystem •

    hapi • Rich Framework • LoopBack • For creating APIs
  11. Node.js Web Frameworks • Express • Minimal, Muture, Ecosystem •

    hapi • Rich Framework • LoopBack • For creating APIs
  12. Node.js Web Frameworks • Express • Minimal, Muture, Ecosystem •

    hapi • Rich Framework • LoopBack • For creating APIs
  13. • Generator based control flow goodness co

  14. • Generator based control flow goodness co

  15. • Generator based control flow goodness co

  16. • Generator based control flow goodness co

  17. • Generator based control flow goodness co Node.js: co: yield

    Yieldable
  18. • Generator based control flow goodness co Python: Tornado: yield

    Future Node.js: co: yield Yieldable
  19. What is koa

  20. What is koa • Node.js Web Framework

  21. What is koa • Node.js Web Framework • Developed by

    Part of Express Team
  22. What is koa • Node.js Web Framework • Developed by

    Part of Express Team • Generators via co
  23. What is koa • Node.js Web Framework • Developed by

    Part of Express Team • Generators via co
  24. What is koa • Node.js Web Framework • Developed by

    Part of Express Team • Generators via co
  25. What is koa • Node.js Web Framework • Developed by

    Part of Express Team • Generators via co • Minimal Core
  26. agenda • What is koa • Before Diving in koa

    • Hello koa • Running koa in Production?
  27. Before Diving in koa

  28. Before Diving in koa • Callback Hell

  29. Callback Hell from http://callbackhell.com/

  30. Before Diving in koa • Callback Hell

  31. Before Diving in koa • Callback Hell • Promise/Deferred

  32. Promise/Deferred • Promise/Deferred • .then() • Resolve • Reject

  33. Promise/Deferred (Cont’d) AngularJS’s promise/deferred implementation inspired by Kris Kowal's Q.

  34. Before Diving in koa • Callback Hell • Promise/Deferred

  35. Before Diving in koa • Callback Hell • Promise/Deferred •

    Generators
  36. Generators

  37. Generators • ECMAScript Harmony (6th Edition)

  38. Generators • ECMAScript Harmony (6th Edition) • Chrome: about:flags ➛

    Enable Experimental JavaScript
  39. Generators • ECMAScript Harmony (6th Edition) • Chrome: about:flags ➛

    Enable Experimental JavaScript • Firefox 33.0
  40. Generators • ECMAScript Harmony (6th Edition) • Chrome: about:flags ➛

    Enable Experimental JavaScript • Firefox 33.0 • Iterator
  41. Generators • ECMAScript Harmony (6th Edition) • Chrome: about:flags ➛

    Enable Experimental JavaScript • Firefox 33.0 • Iterator • next()
  42. Generators • ECMAScript Harmony (6th Edition) • Chrome: about:flags ➛

    Enable Experimental JavaScript • Firefox 33.0 • Iterator • next() • throw StopIteration
  43. Generators • ECMAScript Harmony (6th Edition) • Chrome: about:flags ➛

    Enable Experimental JavaScript • Firefox 33.0 • Iterator • next() • throw StopIteration https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators
  44. Generators • ECMAScript Harmony (6th Edition) • Chrome: about:flags ➛

    Enable Experimental JavaScript • Firefox 33.0 • Iterator • next() • throw StopIteration • yield
  45. Generators • ECMAScript Harmony (6th Edition) • Chrome: about:flags ➛

    Enable Experimental JavaScript • Firefox 33.0 • Iterator • next() • throw StopIteration • yield https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators
  46. Andre Lee next generation web framework for node.js koa

  47. Andre Lee next generation web framework for node.js koa

  48. Before Diving in koa • Callback Hell • Promise/Deferred •

    Generators
  49. Before Diving in koa • Callback Hell • Promise/Deferred •

    Generators • thunkify
  50. thunkify http://jsbin.com/jayup/edit https://github.com/tj/node-thunkify/blob/master/index.js

  51. thunkify https://github.com/tj/node-thunkify/blob/master/index.js

  52. thunkify https://github.com/tj/node-thunkify/blob/master/index.js

  53. thunkify http://jsbin.com/xubajo/edit // 3 // 4 https://github.com/tj/node-thunkify/blob/master/index.js

  54. thunkify http://jsbin.com/xubajo/edit // 3 // 4 // 3 https://github.com/tj/node-thunkify/blob/master/index.js

  55. thunkify co will execute next() when callback http://jsbin.com/xubajo/edit // 3

    // 4 // 3 https://github.com/tj/node-thunkify/blob/master/index.js
  56. Before Diving in koa • Callback Hell • Promise/Deferred •

    Generators • thunkify
  57. Before Diving in koa • Callback Hell • Promise/Deferred •

    Generators • thunkify • Yieldables
  58. Yieldables • promises • thunks (functions) • array (parallel execution)

    • objects (parallel execution) • generators (delegation) • generator functions (delegation)
  59. agenda • What is koa • Before Diving in koa

    • Hello koa • Running koa in Production?
  60. Hello koa

  61. Hello koa • Requires node 0.11.x for the --harmony flag

  62. Hello koa • Requires node 0.11.x for the --harmony flag

    • Application
  63. Hello koa • Requires node 0.11.x for the --harmony flag

    • Application • To provide high level "sugar" at the otherwise low-level middleware layer
  64. Hello koa • Requires node 0.11.x for the --harmony flag

    • Application • To provide high level "sugar" at the otherwise low-level middleware layer • Cascading
  65. Hello koa • Requires node 0.11.x for the --harmony flag

    • Application • To provide high level "sugar" at the otherwise low-level middleware layer • Cascading • Settings
  66. Hello koa • Requires node 0.11.x for the --harmony flag

    • Application • To provide high level "sugar" at the otherwise low-level middleware layer • Cascading • Settings
  67. Hello koa • Requires node 0.11.x for the --harmony flag

    • Application • To provide high level "sugar" at the otherwise low-level middleware layer • Cascading • Settings • app.listen(...)
  68. Hello koa • Requires node 0.11.x for the --harmony flag

    • Application • To provide high level "sugar" at the otherwise low-level middleware layer • Cascading • Settings • app.listen(...) • app.callback()
  69. Hello koa • Requires node 0.11.x for the --harmony flag

    • Application • To provide high level "sugar" at the otherwise low-level middleware layer • Cascading • Settings • app.listen(...) • app.callback() • app.use(function)
  70. Hello koa • Requires node 0.11.x for the --harmony flag

    • Application • To provide high level "sugar" at the otherwise low-level middleware layer • Cascading • Settings • app.listen(...) • app.callback() • app.use(function) • app.keys=
  71. Hello koa • Requires node 0.11.x for the --harmony flag

    • Application • To provide high level "sugar" at the otherwise low-level middleware layer • Cascading • Settings • app.listen(...) • app.callback() • app.use(function) • app.keys= • Error Handling
  72. Hello koa (Cont’d)

  73. Hello koa (Cont’d) • Context

  74. Hello koa (Cont’d) • Context

  75. Hello koa (Cont’d) • Context ctx.req ➛ node’s request ctx.res

    ➛ node’s response ctx.request ➛ koa’s request ctx.response ➛ koa’s response
  76. Hello koa (Cont’d) • Context • MVC

  77. Hello koa (Cont’d) • Context • MVC • koa-* wiki

  78. Hello koa (Demo) • 00_hello_world.js • 01_cascading.js • 02_thunkify.js •

    03_routing.js • 04_error_handling.js • 05_request.js • 06_mongo.js • 07_redis.js • 08_ejs.js
  79. agenda • What is koa • Before Diving in koa

    • Hello koa • Running koa in Production?
  80. Running koa in Production?

  81. Running koa in Production? • TJ for GO lang, Express

    to Strongloop
  82. Running koa in Production? • TJ for GO lang, Express

    to Strongloop • Using co’s ecosystem
  83. Running koa in Production? • TJ for GO lang, Express

    to Strongloop • Using co’s ecosystem • Node.js v0.11.x is unstable
  84. Running koa in Production? • TJ for GO lang, Express

    to Strongloop • Using co’s ecosystem • Node.js v0.11.x is unstable • Generators
  85. Running koa in Production? • TJ for GO lang, Express

    to Strongloop • Using co’s ecosystem • Node.js v0.11.x is unstable • Generators • Better Performance?
  86. Running koa in Production? • TJ for GO lang, Express

    to Strongloop • Using co’s ecosystem • Node.js v0.11.x is unstable • Generators • Better Performance? from https://medium.com/javascript-and-the-server
  87. Running koa in Production? • TJ for GO lang, Express

    to Strongloop • Using co’s ecosystem • Node.js v0.11.x is unstable • Generators • Better Performance?
  88. References • http://callbackhell.com/ • http://www.html-js.com/article/1752 • https://medium.com/javascript-and-the-server/ trying-out-node-0-11-in-production-3d8135e0f3da

  89. Andre Lee ?