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

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

D76231a2114896dfcc7b79ac69558b79?s=128

Yosuke Furukawa

February 26, 2014
Tweet

Transcript

  1.  ͜Ε͔Βͷ/PEFKTͷ࿩ Λ͠Α͏ !ZPTVLF@GVSVLBXB

  2. !ZPTVLF@GVSVLBXB ೔ຊ/PEFKTϢʔβʔάϧʔϓ৽୅ද

  3. !ZPTVLF@GVSVLBXB $VSSFOU.Z803,4 4VQQPSU%FWFMPQFST $SFBUJOH(BNFT $SFBUJOH'SBNFXPSLT

  4. !ZPTVLF@GVSVLBXB $VSSFOUNZJOUFSFTUT 1FSGPSNBODF*NQSPWFNFOU 3FBMUJNF "OZPUIFSOFXUFDIT MJLF(P %PDLFS $MJFOUTJEF.7$ FUD

  5. !ZPTVLF@GVSVLBXB $VSSFOU.ZGBWT 1FSGPSNBODF*NQSPWFNFOU 3FBMUJNF "OZPUIFSOFXUFDIT MJLF(P %PDLFS $MJFOUTJEF.7$ FUD 'PMMPXNF

  6. ຊ୊

  7. ຊ୊ /PEFKTOFXT ؾʹͳΔ/PEFKTϥΠϒϥϦ ౦ژ/PEFֶԂͷࠓޙ

  8. /PEFKTOFXT

  9. /PEFKTOFXT 

  10. /PEFKTOFXT  Ϧʔμʔ͕*[BBDT͔Β 5+'POUBJOFʹ

  11. 5+'POUBJOF͸͜Μͳਓɹ ʹDPSFUFBNʹKPJO $*΍UFTUϝΠϯʹ୲౰ 8BMNBSUͷϝϞϦϦʔΫΛൃݟ ͨ͠Γͱ௿ϨΠϠʔʹ΋ڧ͍

  12. 5+'POUBJOF͸͜Μͳਓɹ ʹDPSFUFBNʹKPJO $*΍UFTUϝΠϯʹ୲౰ 8BMNBSUͷϝϞϦϦʔΫΛൃݟ ͨ͠Γͱ௿ϨΠϠʔʹ΋ڧ͍ ౦ژ/PEFֶԂࡇ ʹݺͼ͍ͨ

  13. /PEFKTOFXT 

  14. /PEFKTOFXT  W΋͏͙͢Ͱ͢Ͷ

  15. WͰ৽͘͠ͳΔ͜ͱ 4USFBN DMVTUFSSPVOESPCJO NVMUJQMFDPOUFYUJOWN TZODDIJME@QSPDFTT USBDJOHBQJ QFSGPSNBODFJNQSPWFNFOU FUD

  16. WͰ৽͘͠ͳΔ͜ͱ 4USFBN DMVTUFSSPVOESPCJO NVMUJQMFDPOUFYUJOWN TZODDIJME@QSPDFTT USBDJOHBQJ QFSGPSNBODFJNQSPWFNFOU FUD ࢒೦ͳ͕ΒWͰ͸&4σϑΥϧτʹ͸ͳΒͳ͍

  17. WͰ৽͘͠ͳΔ͜ͱ ٞ࿦ͷྲྀΕΛϒϩάʹ঺հͯ͠ΔͷͰ͕࣌ؒ͋Ε͹ݟ ͍ͯͩ͘͞ɻ

  18. WͰ৽͘͠ͳΔ͜ͱ 4USFBN DMVTUFSSPVOESPCJO NVMUJQMFDPOUFYUJOWN TZODDIJME@QSPDFTT USBDJOHBQJ QFSGPSNBODFJNQSPWFNFOU FUD

  19. TZODDIJME@QSPDFTT DIJME@QSPDFTTʹҎԼͷ"1*͕௥Ճ͞ΕΔɻ FYFD4ZOD FYFD'JMF4ZOD TQBXO4ZOD

  20. TZODDIJME@QSPDFTT DIJME@QSPDFTTʹҎԼͷ"1*͕௥Ճ͞ΕΔɻ FYFD4ZOD FYFD'JMF4ZOD TQBXO4ZOD HSVOUͱ͔ͷλεΫϥϯφʔͰ خ͍͠ػೳ

  21. TZODDIJME@QSPDFTT %&.0

  22. USBDJOH"1* QSPpMJOH༻ͷ"1* IFBQNFNPSZͷঢ়گΛϞχλͨ͠Γ UJDLຖʹVTFSͷ೚ҙͷΦϒδΣΫτΛUSBDFͰ͖ Δ

  23. USBDJOH"1* %&.0

  24. Wͷશػೳ͸঺հ͠ ͖Εͳ͍

  25. Wͷશػೳ͸঺հ͠ ͖Εͳ͍ 4USPOH-PPQ͕͔ͳΓ ঺հͯ͠ΔͷͰݟΑ͏ /PEFֶԂࡇʹ4USPOH-PPQͷਓ ݺΜͰ΋͍͍͔΋

  26. ؾʹͳΔϥΠϒϥϦ

  27. ؾʹͳΔϥΠϒϥϦ

  28. ؾʹͳΔϥΠϒϥϦ 8"'ฤ

  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';! });
  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 Ͱͷະདྷ
  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;! });!
  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;! });! ΤϥʔϋϯυϦϯά௚ײత
  33. ৄ͘͠͸

  34. ৄ͘͠͸ ๻ͷॻ͍ͨLPBೖ໳هࣄΛ͝ҰಡԼ͍͞ɻ

  35. ؾʹͳΔϥΠϒϥϦ SFBMUJNFฤ

  36. ؾʹͳΔϥΠϒϥϦ SFBMUJNFฤ 4PDLFU*0 ͸৽͍͠1FSMͰ͋Δɻ

  37. SFBMUJNFϥΠϒϥϦ͸ ࣮͸ͨ͘͞Μ͋Δ

  38. SFBMUJNFϥΠϒϥϦ͸ ࣮͸ͨ͘͞Μ͋Δ TPDLFUJPʹ͚ͩ नΘΕΔඞཁͳ͍ɻ

  39. Ͱ΋ͲΕ࢖͏ͷ

  40. 1SJNVT

  41. 1SJNVT ͦ͜Ͱɺ1SJNVTɻ USBOTGFS૚ͰFOHJOFJP GBZF TPDLKT CSPXTFSDIBOOFM XT͕ ࢖͑Δɺͦͷ্ͰTPDLFUJPͰαϙʔτ͍ͯ͠ΔΑ͏ͳ SPPNͱ͔ͷΞϓϦέʔγϣϯ૚Λߏங͢Δ

  42. 1SJNVT ͦ͜Ͱɺ1SJNVTɻ USBOTGFS૚ͰFOHJOFJP GBZF TPDLKT CSPXTFSDIBOOFM͕ ࢖͑Δɺͦͷ্ͰTPDLFUJPͰαϙʔτ͍ͯ͠ΔΑ͏ͳ SPPNͱ͔ͷΞϓϦέʔγϣϯ૚Λߏங͢Δ ೰ΜͰͳ͍Ͱ1SJNVT࢖͓͏ɺ ޙͰUSBOTGFS૚͸ม͑ΒΕΔɻ

  43. ؾʹͳΔϥΠϒϥϦ ϑϩϯτΤϯυπʔϧฤ

  44. ؾʹͳΔϥΠϒϥϦ ϑϩϯτΤϯυฤ ඦՖ៶ཚ͗ͯ͢ɺઆ໌͸ׂѪɻ

  45. ؾʹͳΔϥΠϒϥϦ ϑϩϯτΤϯυπʔϧฤ ݸਓతʹ͸HVMQ CSPXTFSJGZ DPNQPOFOUJP

  46. ͜ͷޙCSPXTFSJGZͷ࿩ ͋Γ·͢ʂʂɹ

  47. ౦ژ/PEFֶԂͷࠓޙ

  48. جຊํ਑ ೋ݄ʹҰ౓͸/PEFֶԂΛ։࠵ͯ͠ɺΈΜͳͷ ஌ݟΛूΊ͍ͨ ೥ʹҰ౓ɺ౦ژ/PEFֶԂࡇ͸։࠵͍ͨ͠ /PEFKTͷ࿩͹͔Γ͡Όͳͯ͘ผͳݴޠ͔Βֶ ΜͩΓɺ৽͍͠ࣄ͸ͲΜͲΜٵऩ͍ͨ͠

  49. جຊํ਑ ೋ݄ʹҰ౓͸/PEFֶԂΛ։࠵ͯ͠ɺΈΜͳͷ ஌ݟΛूΊ͍ͨ ೥ʹҰ౓ɺ౦ژ/PEFֶԂࡇ͸։࠵͍ͨ͠ /PEFKTͷ࿩͹͔Γ͡Όͳͯ͘ผͳݴޠ͔Βֶ ΜͩΓɺ৽͍͠ࣄ͸ͲΜͲΜٵऩ͍ͨ͠ ͭ·Γ͜Ε·Ͱ௨Γ /PEFֶԂΛ΍Γ͍ͨ

  50. ม͍͑ͨ͜ͱ ΋ͬͱελοϑҎ֎͔Βͷਓͷҙݟ΋ฉ͖͍ͨɻ ΈΜͳ͕ͲΜͳͷʹڵຯ͕༗Δͷ͔ืू͍ͨ͠ɻ HJUUFSͱHJUIVCϦϙδτϦ࡞Γ·ͨ͠ɻ
 IUUQTHJUUFSJNZPTVLFGVSVLBXB5PLZP/PEF'FTUJWBM
 IUUQTHJUIVCDPNZPTVLFGVSVLBXB5PLZP/PEF'FTUJWBM

  51. ม͍͑ͨ͜ͱ ΋ͬͱελοϑҎ֎͔Βͷਓͷҙݟ΋ฉ͖͍ͨɻ ΈΜͳ͕ͲΜͳͷʹڵຯ͕༗Δͷ͔ืू͍ͨ͠ɻ HJUUFSͱHJUIVCϦϙδτϦ࡞Γ·ͨ͠ɻ
 IUUQTHJUUFSJNZPTVLFGVSVLBXB5PLZP/PEF'FTUJWBM
 IUUQTHJUIVCDPNZPTVLFGVSVLBXB5PLZP/PEF'FTUJWBM Θ͍Θ͍΍Γ͍ͨ

  52. ·ͱΊ

  53. ·ͱΊ /PEFKTOFXT Ϧʔμʔมߋ J[T5+'POUBJOF  /PEFKTWͰมΘΔ͜ͱ ؾʹͳΔ/PEFKTϥΠϒϥϦ ,PB 1SJNVT ϑϩϯτΤϯυπʔϧ͋Γա͗

    ౦ژ/PEFֶԂͷࠓޙ جຊ͸ม͑ͳ͍ ҙݟΛऩू͠΍͘͢ɺ൓ө͠΍͘͢͢Δ Θ͍Θ͍Θʔ͍
  54. ೋ୅໨ऻ໊൸࿐ޱ্

  55. ͍ͭ·Ͱ΋มΘΒ͵NodeֶԂ΁ͷޚް৘ɺ͝᩾ርΛ ۱͔Β۱·Ͱ ͣͣͣʔ͍ͱ ޤ͍ئ͍ ͋͛ͨͯ·ͭΓ·͢Δʂʂʂ

  56. ͍ͭ·Ͱ΋มΘΒ͵NodeֶԂ΁ͷޚް৘ɺ͝᩾ርΛ ۱͔Β۱·Ͱ ͣͣͣʔ͍ͱ ޤ͍ئ͍ ͋͛ͨͯ·ͭΓ·͢Δʂʂʂ ΑΖ͓͘͠ئ͍͠·͢