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

これからのNode.jsの話をしよう

 これからのNode.jsの話をしよう

Yosuke Furukawa

February 26, 2014
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. ؾʹͳΔϥΠϒϥϦ 8"'ฤ // 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';! });
  2. ؾʹͳΔϥΠϒϥϦ 8"'ฤ // 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';! }); HFOFSBUPSZJFME Ͱͷະདྷ
  3. ؾʹͳΔϥΠϒϥϦ 8"'ฤ // handle error! app.use(function *(next) {! // try-catchͰΤϥʔϋϯυϦϯάͰ͖Δ!

    try {! yield next;! } catch (err) {! this.status = 500;! this.body = err.message;! }! });! ! // error thrower! app.use(function *(next) {! if (this.url === "/_err") {! throw new Error("Send Error");! }! yield next;! });!
  4. ؾʹͳΔϥΠϒϥϦ 8"'ฤ // handle error! app.use(function *(next) {! // try-catchͰΤϥʔϋϯυϦϯάͰ͖Δ!

    try {! yield next;! } catch (err) {! this.status = 500;! this.body = err.message;! // errorΠϕϯτΛൃߦ! this.app.emit("error", err, this);! }! });! ! // error thrower! app.use(function *(next) {! if (this.url === "/_err") {! throw new Error("Send Error");! }! yield next;! });! ΤϥʔϋϯυϦϯά௚ײత