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

koa

 koa

Getting started with koa

Yosuke Furukawa

March 09, 2014
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. ͍͖ͳΓͷ)FMMP8PSME var koa = require('koa');! var app = koa();! !

    // ͕͜͜ಛ௃తɺfunction * ͰgeneratorΛར༻ͯ͠ ͍Δɻ! app.use(function *(){! this.body = 'Hello World';! });! ! app.listen(3000);
  2. LPBͷεΫϦϓτΛ࣮ߦ ͢Δͱ͖ $ node version! // v0.11.9Ҏ߱Ͱ͋Δඞཁ͕͋Δ! $ node --harmony

    app.js! // ͜͜ॏཁ harmony ΦϓγϣϯΛ෇͚Δඞཁ͕͋Δ! // goto http://localhost:3000/! ! $ curl http://localhost:3000/! // Hello World
  3. LPBͷεΫϦϓτΛ࣮ߦ ͢Δͱ͖ $ node version! // v0.11.9Ҏ߱Ͱ͋Δඞཁ͕͋Δ! $ node --harmony

    app.js! // ͜͜ॏཁ harmony ΦϓγϣϯΛ෇͚Δඞཁ͕͋Δ! // goto http://localhost:3000/! ! $ curl http://localhost:3000/! // Hello World ΍ͬͨͶʂʂ
  4. ϛυϧ΢ΣΞΛ૊Έ߹ΘͤΔ var koa = require(‘koa');! // ϧʔλʔɺ੩తϑΝΠϧ഑৴ɺϨϯμϦϯάΤϯδϯΛઃఆ! var route =

    require('koa-route');! var serve = require('koa-static');! var views = require('co-views');! var app = koa();! 
 // jadeΛςϯϓϨʔτΤϯδϯͱͯ͠ઃఆɻ! var render = views(__dirname + '/views', { map : {html : 'jade'}});! // GET /views => render template engine! app.use(route.get('/views', function *(next) {! // bodyʹରͯ͠index.jadeͷมߋΛ࣮ࢪɻ! this.body = yield render('index.jade', {name: "koa"});! }));! // GET /hello => 'Hello!'! app.use(route.get('/hello', function *() {! this.body = 'Hello!!';! }));! // GET /hello/:name => 'Hello :name'! app.use(route.get('/hello/:name', function *(name) {! this.body = 'Hello ' + name;! }));! // static file serve! app.use(serve(__dirname + '/public'));! ! app.listen(3000);
  5. ϛυϧ΢ΣΞΛ૊Έ߹ΘͤΔ var koa = require(‘koa');! // ϧʔλʔɺ੩తϑΝΠϧ഑৴ɺϨϯμϦϯάΤϯδϯΛઃఆ! var route =

    require('koa-route');! var serve = require('koa-static');! var views = require('co-views');! var app = koa();! 
 // jadeΛςϯϓϨʔτΤϯδϯͱͯ͠ઃఆɻ! var render = views(__dirname + '/views', { map : {html : 'jade'}});! // GET /views => render template engine! app.use(route.get('/views', function *(next) {! // bodyʹରͯ͠index.jadeͷมߋΛ࣮ࢪɻ! this.body = yield render('index.jade', {name: "koa"});! }));! // GET /hello => 'Hello!'! app.use(route.get('/hello', function *() {! this.body = 'Hello!!';! }));! // GET /hello/:name => 'Hello :name'! app.use(route.get('/hello/:name', function *(name) {! this.body = 'Hello ' + name;! }));! // static file serve! app.use(serve(__dirname + '/public'));! ! app.listen(3000); WJFXT IFMMP IFMMPKTDBGF
 ͱ͔ʹߦͬͯΈΑ͏
  6. ϛυϧ΢ΣΞΛ૊Έ߹ΘͤΔ var koa = require(‘koa');! // ϧʔλʔɺ੩తϑΝΠϧ഑৴ɺϨϯμϦϯάΤϯδϯΛઃఆ! var route =

    require('koa-route');! var serve = require('koa-static');! var views = require('co-views');! var app = koa();! 
 // jadeΛςϯϓϨʔτΤϯδϯͱͯ͠ઃఆɻ! var render = views(__dirname + '/views', { map : {html : 'jade'}});! // GET /views => render template engine! app.use(route.get('/views', function *(next) {! // bodyʹରͯ͠index.jadeͷมߋΛ࣮ࢪɻ! this.body = yield render('index.jade', {name: "koa"});! }));! // GET /hello => 'Hello!'! app.use(route.get('/hello', function *() {! this.body = 'Hello!!';! }));! // GET /hello/:name => 'Hello :name'! app.use(route.get('/hello/:name', function *(name) {! this.body = 'Hello ' + name;! }));! // static file serve! app.use(serve(__dirname + '/public'));! ! app.listen(3000); ͱ͜ΖͰ͜ͷZJFMEͬͯԿ  ࣍εϥΠυͰઆ໌͠·͢
  7. FYQSFTTͰϛυϧ΢ΣΞΛॻ͘ // Express! // response time! app.use(function(req, res, next){! var

    start = new Date;! res.on('header', function(){! var ms = new Date - start;! res.setHeader('X-Response-Time', ms + 'ms');! });! next();! }); ! ! // response! app.use(function(req, res, next){! res.send(200, "Hello World");! });
  8. FYQSFTTͰϛυϧ΢ΣΞΛॻ͘ // Express! // response time! app.use(function(req, res, next){! var

    start = new Date;! res.on('header', function(){! var ms = new Date - start;! res.setHeader('X-Response-Time', ms + 'ms');! });! next();! }); ! ! // response! app.use(function(req, res, next){! res.send(200, "Hello World");! }); OFYUؔ਺Ͱ࣍ͷϛυϧ΢ΣΞ Λݺͼग़͢ SFR SFT OFYUͷҾ਺Λड͚औΔ SFTQPOTFͷΠϕϯτΛड͚ औͬͯɺ࣌ؒΛܭࢉ͢Δ
  9. LPBͰϛυϧ΢ΣΞΛॻ͘ // koa! // response time! app.use(function *(next){! var start

    = new Date;! yield next;! var ms = new Date - start;! this.set('X-Response-Time', ms + 'ms');! });! ! // response! app.use(function *(){! this.body = 'Hello World';! });
  10. LPBͰϛυϧ΢ΣΞΛॻ͘ // koa! // response time! app.use(function *(next){! var start

    = new Date;! yield next;! var ms = new Date - start;! this.set('X-Response-Time', ms + 'ms');! });! ! // response! app.use(function *(){! this.body = 'Hello World';! }); GVODUJPO ͰHFOFSBUPS GVODUJPOؔ਺Λఆٛ͢Δ ZJFMEΛݺͿ͜ͱΛએݴ ZJFMEOFYUͰ࣍ͷNJEEMFXBSF Λݺͼग़͢
  11. HFOFSBUPSZJFME // koa! // response time! app.use(function *(next){! var start

    = new Date;! yield next;! var ms = new Date - start;! this.set('X-Response-Time', ms + 'ms');! });! ! // response! app.use(function *(){! this.body = 'Hello World';! }); IFBEFSΠϕϯτͷड৴͸ෆཁɻ ͜ͷ࣌ʹ͸طʹSFTQPOTF͕ฦͬͯΔ
  12. FYQSFTTWTLPB // Express! ! // response time! ! app.use(function(req, res,

    next){! var start = new Date;! res.on('header', function(){! var ms = new Date - start;! res.setHeader('X-Response-Time', ms + 'ms');! });! next();! }); ! ! // response! ! app.use(function(req, res, next){! res.send(200, "Hello World");! }); // koa! ! // response time! ! app.use(function *(next){! var start = new Date;! yield next;! var ms = new Date - start;! this.set('X-Response-Time', ms + 'ms');! });! ! // response! ! app.use(function *(){! this.body = 'Hello World';! });
  13. // koa! ! // response time! ! app.use(function *(next){! var

    start = new Date;! yield next;! var ms = new Date - start;! this.set('X-Response-Time', ms + 'ms');! });! ! // response! ! app.use(function *(){! this.body = 'Hello World';! }); // Express! ! // response time! ! app.use(function(req, res, next){! var start = new Date;! res.on('header', function(){! var ms = new Date - start;! res.setHeader('X-Response-Time', ms + 'ms');! });! next();! }); ! ! // response! ! app.use(function(req, res, next){! res.send(200, "Hello World");! }); FYQSFTTWTLPB ϙΠϯτ͸ඇಉظॲཧΛ ಉظͬΆ͘ॻ͚Δͱ͜Ζ BWPJEDBMMCBDLIFMM
  14. FYQSFTTͰΤϥʔॲཧ // Express! // error thrower! app.use(function(req, res, next){! if

    (req.url === "/_err") {! next(new Error("abc"));! }! }); ! ! // handle error! app.use(function(err, req, res, next){! if (err) {! console.error(err.stack);! res.send(500, err.message);! }! });
  15. FYQSFTTͰΤϥʔॲཧ // Express! // error thrower! app.use(function(req, res, next){! if

    (req.url === "/_err") {! next(new Error("abc"));! }! }); ! ! // handle error! app.use(function(err, req, res, next){! if (err) {! console.error(err.stack);! res.send(500, err.message);! }! }); FSS SFR SFT OFYUͷ Ҿ਺Λड͚औΔ JGͰFSSͷ༗ແΛݟͯ Τϥʔॲཧ
  16. LPBͰΤϥʔॲཧ // handle error! app.use(function *(next) {! try {! yield

    next;! } catch (err) {! console.error(err.stack);! this.status = 500;! this.body = err.message;! }! });! ! // error thrower! app.use(function *(next) {! if (this.url === "/_err") {! throw new Error("Send Error");! }! yield next;! });
  17. LPBͰΤϥʔॲཧ // handle error! app.use(function *(next) {! try {! yield

    next;! } catch (err) {! console.error(err.stack);! this.status = 500;! this.body = err.message;! }! });! ! // error thrower! app.use(function *(next) {! if (this.url === "/_err") {! throw new Error("Send Error");! }! yield next;! }); USZ DBUDIͰඇಉظॲཧΛ ΤϥʔϋϯυϦϯάͰ͖Δ
  18. FYQSFTTWTLPB // Express! ! // error thrower! app.use(function(req, res, next)

    {! if (req.url === "/_err") {! next(new Error("abc"));! }! });! ! // handle error! app.use(function(err, req, res, next){! if (err) {! console.error(err.stack);! res.send(500, err.message);! }! }); // koa! ! // handle error! app.use(function *(next) {! try {! yield next;! } catch (err) {! console.error(err.stack);! this.status = 500;! this.body = err.message;! }! });! ! // error thrower! app.use(function *(next) {! if (this.url === "/_err") {! throw new Error("Send Error");! }! yield next;! });
  19. FYQSFTTWTLPB // Express! ! // error thrower! app.use(function(req, res, next)

    {! if (req.url === "/_err") {! next(new Error("abc"));! }! });! ! // handle error! app.use(function(err, req, res, next){! if (err) {! console.error(err.stack);! res.send(500, err.message);! }! }); // koa! ! // handle error! app.use(function *(next) {! try {! yield next;! } catch (err) {! console.error(err.stack);! this.status = 500;! this.body = err.message;! }! });! ! // error thrower! app.use(function *(next) {! if (this.url === "/_err") {! throw new Error("Send Error");! }! yield next;! }); ϙΠϯτ͸ඇಉظॲཧͰ΋ USZDBUDIͰΤϥʔॲཧͰ͖Δ ͱ͜Ζʂʂʂ