Node.js‹‰¼·¼ Framework Koa

download Node.js‹‰¼·¼ Framework Koa

of 35

  • date post

    08-Sep-2014
  • Category

    Technology

  • view

    6.545
  • download

    6

Embed Size (px)

description

Node.js勉強会 Framework Koaについてのスライドです

Transcript of Node.js‹‰¼·¼ Framework Koa

  • Framework Koa Node.js 2014.2.8
  • (Twitter@kamiyam) http://nantokaworks.com Engineer Node.js
  • Framework Koa
  • Koa 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. Through leveraging generators Koa allows you to ditch callbacks and greatly increase error-handling. Koa does not bundle any middleware within core, and provides an elegant suite of methods that make writing servers fast and enjoyable. Via. http://koajs.com/
  • Express Express Node.js FrameworkExpress
  • Express? Express(Node.js/JavaScript) ( )
  • () Hello JavaScript??
  • // document ready $(function(){ var $target = $(".initAnimetion); $target.fadeIn( "slow", function() { setTimeout(function(){ $target.fadeOut( "slow", function() { console.log( "completed!!!" ); }) }), 2*1000 ); //2 }); }); Hello Callback Hell!!! http://callbackhell.com/ http://codepen.io/kamiyam/details/kDxrw
  • ! var result= getHttpResponse(http://example.com); var geo = getGeoLocale( result.local ); response.send( geo.json );
  • Koa? generator/yield middleware
  • Koa To use Koa you must be running node 0.11.9 or higher for generator support $ alias node='node --harmony' package.json "scripts": { "start": "node --harmony app.js" }
  • Koa sample var koa = require('koa'), app = koa(); app.use(function *() { // Here is the important bit of application logic for this example. // We make use of a series of async operations without callbacks. var city = yield geolocation.getCityAsync(this.req.ip); var forecast = yield weather.getForecastAsync(city); this.body = 'Today, ' + city + ' will be ' + forecast.temperature + ' degrees.'; }); app.listen(8080); Via. http://blog.stevensanderson.com/2013/12/21/ experiments-with-koa-and-javascript-generators/
  • ?JavaScript?
  • Koa sample app.use(function *() { var tasks = { imposeMinimumResponseTime: delay(500), fetchWebPage: doHttpRequest('http://example.com/'), squareTenSlowly: getSquareValueThunk(10) }; // All of the operations have already started. Yielding // the key-value object to Koa just makes it wait for them. var results = yield tasks; this.body = 'OK, all done.'; this.body += 'nResult of waiting is: ' + results.imposeMinimumResponseTime; // Displays: undefined this.body += 'nWeb page status code is: ' + results.fetchWebPage.statusCode; // Displays: Typically 200 in this case this.body += 'nSquare of ten is: ' + results.squareTenSlowly; // Displays: 100 }); Via. http://blog.stevensanderson.com/2013/12/21/ experiments-with-koa-and-javascript-generators/
  • generator/yield?
  • generator/yield generator/yield EcmaScrpt6 (ES6)
  • generator/yield ! function *getAllSquareNumbers() { for (var i = 1; ; i++) { yield i * i; } } ! var generator = getAllSquareNumbers(); console.log(generator.next().value); // console.log(generator.next().value); // console.log(generator.next().value); // console.log(generator.next().value); // Outputs Outputs Outputs Outputs '1' '4' '9' '16' Via. http://blog.stevensanderson.com/2013/12/21/ experiments-with-koa-and-javascript-generators/
  • generator/yield Iterator
  • middleware?
  • middleware middlewareWeb ) Expressconnect
  • ghost 301 redirect //for wordpress permlink import articles app.use(function redirect301 ( req, res, next ){ ! ! var querys = { p: req.query["p"], page: req.query["page_id"] } // console.log("access to: " + req.headers.host); if ( querys.p || querys.page ){ var redirect = "http://" + req.headers.host; if ( querys.p ){ redirect += "/p" + querys.p + ""; } else if( querys.page ){ redirect += "/page" + querys.page + ""; } // console.log("301 redirect: " + redirect + req.headers.url + "=>" + redirect ); res.writeHead(301, {"Location": redirect, 'Expires': (new Date).toGMTString()}); res.end(); https://gist.github.com/kamiyam/649fb9c12ef83ced920b ! }); } else { next(); }
  • Via. http://techblog.yahoo.co.jp/programming/javascript_error/
  • middleware request Y^Y^Y^Y^Y^Y^Y^Y^Y next(); middleware next(); middleware error next(); middleware error response response response response middleware
  • Koa log middleware // x-response-time ! app.use(function *(next){ var start = new Date; yield next; var ms = new Date - start; this.set('X-Response-Time', ms + 'ms'); }); Via. http://dailyjs.com/2014/01/09/koa/
  • middleware request yield next; yield next; yield next; middleware middleware middleware try catch(e) error error var start = new Date; var ms = new Date - start; response response response response middleware error
  • generator/yieldKoa
  • Express ? Koa ( Node.js v0.12.x Generator ) koa Express/Connect
  • Koa Generetor/Yield Koa Generetor/Yield
  • Koa koa-static koa-route URL co-views View etc...
  • Koa Generator/Yield Generator/Yield Iterator Koamiddleware Cascading/upstream
  • Koa from scratch - koa http://yosuke-furukawa.hatenablog.com/entry/2013/12/26/125351 DailyJS - Is Koa the Future of Node Frameworks? http://dailyjs.com/2014/01/09/koa/ - Web Koa http://blog.kazupon.jp/post/71041135220/koa Steven Sanderson's blog - Experiments with Koa and JavaScript Generators http://blog.stevensanderson.com/2013/12/21/experiments-with-koa-and-javascript-generators/ Flowery - KoaJavaScript Experiments with Koa and JavaScript Generators http://yukihr.github.io/blog/2013/12/31/experiments-with-koa-and-javascript-generators-ja/
  • Generator/Yield - Node.js 0.12 yield http://tech.nitoyon.com/ja/blog/2013/06/27/node-yield/ PHP5.5 by @kwatch http://www.slideshare.net/kwatch/php55 Python Snippets -yield http://python.civic-apps.com/generator/ JavaScript http://techblog.yahoo.co.jp/programming/javascript_error/