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

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

D76231a2114896dfcc7b79ac69558b79?s=128

Yosuke Furukawa

February 26, 2014
Tweet

Transcript

  1. 6.
  2. 29.

    ؾʹͳΔϥΠϒϥϦ 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';! });
  3. 30.

    ؾʹͳΔϥΠϒϥϦ 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 Ͱͷະདྷ
  4. 31.

    ؾʹͳΔϥΠϒϥϦ 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;! });!
  5. 32.

    ؾʹͳΔϥΠϒϥϦ 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;! });! ΤϥʔϋϯυϦϯά௚ײత
  6. 33.
  7. 40.
  8. 52.