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

Node.js入門

 Node.js入門

インストールからモジュールによる拡張まで!

Caf2a7eb5e2354b16dac974a445464d6?s=128

badatmath

April 01, 2012
Tweet

Transcript

  1. Node.js introduction Yusuke Shinozaki @bad_at_math

  2. ࣗݾ঺հ ˎུˎ

  3. ͓͸ͳ͢͠Δ͜ͱ ͳͥ/PEFKTʁ Πϯετʔϧˍϓϩάϥϛϯά ςετΛ࢖ͬͯΒͪ͘Μʹ ֦ுͰύϫϑϧʹ

  4. ४උͰ͖ͨʁ

  5. /PEFKTͱ͸7ͱ͍͏(PPHMFࣾ੡+BWB4DSJQU ΤϯδϯΛར༻ͨ͠ɺΠϕϯτۦಈ͔ͭϊϯϒ ˎཧ࿦͸ུˎ /PEFKTͱ͸Կ͔ ˎ΋͠ˎڵຯ͕͋Ε͹ IUUQEIBUFOBOFKQCBEBUNBUI

  6. ͳͥ/PEFKTʁ ίΠπͷυί͕ ͍͍͔ͬͯʁ

  7. ͳͥ/PEFKTʁ ͪΐͬͱલ·Ͱ )5.-ͷϨϯμϦϯά͸ 1)1΍3BJMT SVCZ ͳͲ ͕୲͏ྖҬ

  8. ͳͥ/PEFKTʁ ࠷ۙͷ͓࿩ "KBYͷ׆༻΋ਐΜͰ͖ͨ

  9. ͳͥ/PEFKTʁ ࠷ۙͷ͓࿩ )5.-ͷϨϯμϦϯά͸ ΫϥΠΞϯτʢϒϥ΢βʣ Ͱ΍ͬͪΌ͏ɻ

  10. ͳͥ/PEFKTʁ ࠷ۙͷ͓࿩ έʔλΠͩͱ௨৴͸ +40/͚ͩͷ৔߹΋

  11. ͳͥ/PEFKTʁ ΑΓଟ਺ͷΫϥΠΞϯτ ΁ͷରԠ͕ٻΊΒΕΔ ࠷ۙͷ͓࿩

  12. ͳͥ/PEFKTʁ Ͱɺ ܰྔ͔ͭߴੑೳͳ"1* αʔόͷग़ݱ͕଴ͨΕͨ

  13. ͳͥ/PEFKTʁ ͦΜͳ࣌ʹ ઈົͳλΠϛϯάͰ /PEFKT͕ग़ݱ

  14. ͳͥ/PEFKTʁ /PEFͷੑೳ ଟ਺઀ଓ࣌ͷੑೳΑ͠ $16ɺϝϞϦʹ΋༏͍͠

  15. ͳͥ/PEFKTʁ ͔͠΋ +BWB4DSJQUͰॻ͚ͪΌ͏

  16. ͳͥ/PEFKTʁ ͍͍Μ͡ΌͶʁ

  17. ·ͣॳΊʹͩͳ Πϯετʔϧ

  18. Πϯετʔϥ όʔδϣϯ ؅ཧγεςϜ ίϯύΠϧ Πϯετʔϧ

  19. Πϯετʔϧ ൺֱ ։ൃ؀ڥ Πϯετʔϧ ϥΠϒϥϦ Πϯετʔϧ /PEF Πϯετʔϧ Πϯετʔϥ όʔδϣϯ

    ؅ཧγεςϜ ίϯύΠϧ ͜Ε͚ͩ Πνόϯ؆୯ʂ
  20. Πϯετʔϧ Ͱ΋ͳɺΠϯετʔϥ ࢖͏ͱ࢖͑ͳ͍αʔυ ύʔςΟϞδϡʔϧ΋ ͋ΔΜͩɻೖ໳ऴ͑ͨΒ ։ൃ؀ڥͷߏஙʹ τϥΠʂ

  21. ϓϩάϥϛϯά ॳΊͯͷΞϓϦ

  22. var http = require('http'); var server = http.createServer(function(req,res){ ! res.writeHead(200,

    {"Content-Type": "text/plain"}); ! res.end('Hello World'); }); server.listen(9000, function(){ ! server.on('connection', function(socket){ ! ! console.log('client connected'); ! }) }); )FMMP8PSME͔Β ϓϩάϥϛϯά
  23. var http = require(‘http’); ·ͣ͸̍ߦ໨ ϓϩάϥϛϯά

  24. http://nodejs.jp/nodejs.org_ja/api/http.html ϚχϡΞϧΛௐ΂Δ ϓϩάϥϛϯά

  25. ϓϩάϥϛϯά SFRVJSF

  26. Ϟδϡʔϧʁ ϓϩάϥϛϯά

  27. ίΞϞδϡʔϧ ϓϩάϥϛϯά

  28. http.createServer(function(req,res){ Α͏΍࣍͘ͷߦ΁ʂ ϓϩάϥϛϯά

  29. http.createServer(function(req,res){ ؔ਺ͷҾ਺ʹ͍ͭͯ ϓϩάϥϛϯά

  30. ϓϩάϥϛϯά server.listen(9000, function(){ MJTUFOͬͯͳΜͧʁ

  31. ϓϩάϥϛϯά MJTUFOͬͯͳΜͧʁ server.listen(9000, function(){

  32. server.on(‘connection’, function(sock){ ϓϩάϥϛϯά PO ͱΠϕϯτ

  33. ϓϩάϥϛϯά PO ͱΠϕϯτ

  34. ϓϩάϥϛϯά PO ͱΠϕϯτ

  35. ϓϩάϥϛϯά PO ͱΠϕϯτ

  36. Ҿ༻͠·͘Γ αʔηϯ

  37. ϓϩάϥϛϯά ϓϩάϥϜͷ࣮ߦ

  38. ͍·ͦ͜ɺΞΠπͱ ༑ୡʹͳΔνϟϯε

  39. None
  40. J1BE͡Όͳ͍Αʁ

  41. ʼʊ ΞϓϦέʔγϣϯϢʔςΟϦςΟλʔϛφϧBQQ λʔϛφϧ͞· Կ͔༻ʁ

  42. ʼʊ ୭͕ΩϞ͍ͬͯʁ

  43. IUUQDJP Ͱ΋ࠓͳΒ $MPVEͬͯͷ͕ ͋Γ·ͯ͠

  44. ͦͬͪΛ࢖͏ͷ ΋ָͪΜ͔΋

  45. ςετͰָΛ͢Δ ςετΛݏ͏ͷ͸ খֶੜ·Ͱͩͬʂ

  46. var http = require('http'); var server = http.createServer(function(req,res){ ! res.writeHead(200,

    {"Content-Type": "text/plain"}); ! res.end('Hello World'); }); server.listen(9000, function(){ ! server.on('connection', function(socket){ ! ! console.log('client connected'); ! }) }); ॻ͘ ςετ͢Δ मਖ਼͢Δ ϒϥ΢βͰςετ ςετͰָΛ͢Δ αʔόىಈ ʼʊ
  47. /PEFͰςετ ॻ͘ ࣗಈςετ मਖ਼͢Δ αʔόىಈ ʼʊ ςετͰָΛ͢Δ var http =

    require('http'); var server = http.createServer(function(req,res){ ! res.writeHead(200, {"Content-Type": "text/plain"}); ! res.end('Hello World'); }); server.listen(9000, function(){ ! server.on('connection', function(socket){ ! ! console.log('client connected'); ! }) });
  48. var http = require('http'); var cnt = 0; var logm

    = function(str){ console.log('\033[32m S: ' + str + '\033[39m'); }; var server = exports.server = http.createServer(function(req,res){ logm('transmitting response header'); res.writeHead(200, {"Content-Type": "text/plain"}); logm('transmitting response body'); res.end('Hello World'); }); server.on('connection', function(socket){ cnt++; logm('client connected'); logm('sum: '+cnt); }); /* 3rd party modules */ var hello = require('./mod_server'); /* standard modules */ var http = require('http'), assert = require('assert'); /* config */ var PORT = 9000, options = { host: 'localhost', port: PORT, path: '/', method: 'GET' }; /* variables */ var server_response = ''; /* functions */ var logm = function(str){ // yellow console.log('\033[33m C: ' + str + '\033[39m'); }; /* * Error log */ var loge = function(str){ // cyan console.error('\036[33m CError: ' + str + '\033[39m'); }; /* code starts here */ hello.server.listen(PORT, function(){ console.log('starting server kicking by client'); /* using http.get() is more easier */ var req = http.request(options, function(res){ logm('status: '+res.statusCode); assert.equal(200, res.statusCode, 'status code should be 200'); res.on('data', function(chunk) { logm('body: '+chunk); server_response += chunk; }); res.on('end', function(){ logm('server response ended'); hello.server.close(); }); }); //.on('error', function(e){console.error('>>>>');}).end(); req.end(); req.on('error', function(e){ loge('an error occured: '); loge(e); }); }); /* assert response */ process.on('exit', function(){ assert.equal('Hello World', server_response); console.log('\033[7m test passed \033[27m'); }); ςετͰָΛ͢Δ ղઆʂ
  49. ςετͰָΛ͢Δ ίʔυΦΩό IUUQTHJTUHJUIVCDPN IUUQTHJTUHJUIVCDPN

  50. ςετͰָΛ͢Δ ࠓճͷςετର৅ αʔό ΫϥΠΞϯτ εςʔλείʔυ ϨεϙϯεϘσΟ

  51. ςετͰָΛ͢Δ ςετର৅ assert = require('assert'); BTTFSUϞδϡʔϧɺ௒ॏཁʂ

  52. ςετͰָΛ͢Δ ςετର৅ /* code starts here */ hello.server.listen(PORT, function(){ console.log('starting

    server kicking by client'); ςετεΫϦϓτͷதͰαʔόΛىಈ
  53. ςετͰָΛ͢Δ ςετର৅ var req = http.request(options, function(res){ IUUQΫϥΠΞϯτͱͯ͠ϦΫΤετ͢Δ Ͳ͜ʹϦΫΤετ͢Δ͔͸PQUJPOTͷதͰࢦఆ

  54. ςετͰָΛ͢Δ ςετର৅ assert.equal(200, res.statusCode, 'status code should be 200'); ϨεϙϯεͷεςʔλείʔυΛBTTFSUFRVBM

    ͰνΣοΫ͢Δ
  55. ςετͰָΛ͢Δ ςετର৅ res.on('data', function(chunk) { logm('body: '+chunk); server_response += chunk;

    }); ϨεϙϯεϘσΟΛશͯ TFSWFS@SFTQPOTFʹ֨ೲ
  56. ςετͰָΛ͢Δ ςετର৅ /* assert response */ process.on('exit', function(){ assert.equal('Hello World',

    server_response); console.log('\033[7m test passed \033[27m'); }); ऴྃ࣌ʹϨεϙϯεϘσΟͷ಺༰ΛνΣοΫ
  57. ςετͰָΛ͢Δ ;ʔɺۃָۃָ

  58. ֦ுͰύϫϑϧʹ શ։Ͱߦ͜͏θ

  59. http://npmjs.org/ ֦ுͰύϫϑϧʹ OQN(node package manager)

  60. ଞͷݴޠ $PSF module plugin library Node $PSF NPEVMF QBDLBHF ֦ுͰύϫϑϧʹ

    Node <3 modules
  61. ֦ுͰύϫϑϧʹ Ϟδϡʔϧͷݟ͚ͭํ npm registry (search.npmjs.org)

  62. ֦ுͰύϫϑϧʹ Ϟδϡʔϧͷݟ͚ͭํ node-toolbox (toolbox.no.de)

  63. ֦ுͰύϫϑϧʹ Ϟδϡʔϧͷݟ͚ͭํ nipster (eirikb.github.com/nipster/)

  64. ֦ுͰύϫϑϧʹ Ϟδϡʔϧͷݟ͚ͭํ *3$Ͱฉ͘ͷ΋ख OPEFKT

  65. ֦ுͰύϫϑϧʹ Ϟδϡʔϧͷݟ͚ͭํ .-Ͱฉ͘ͷ΋खʢOPEFKT@KQʣ

  66. { ! "name": "firstApp", ! "version": "0.0.1", ! "dependencies": {

    ! ! "express": "" ! } } ֦ுͰύϫϑϧʹ ϞδϡʔϧͷΠϯετʔϧํ๏ package.jsonϑΝΠϧΛॻ͘ npm installίϚϯυͰΠϯετʔϧ Φεεϝʂ
  67. IUUQFYQSFTTKTDPN ֦ுͰύϫϑϧʹ ࢖ͬͯΈΑ͏

  68. var app = exports.server = require('express').createServer(); app.get('/', function(req, res){ res.send('Hello

    World'); }); var http = require('http'); var server = exports.server = http.createServer(function(req,res){ res.writeHead(200, {"Content-Type": "text/plain"}); res.end('Hello World'); }); ֦ுͰύϫϑϧʹ ࢖ͬͯΈΑ͏
  69. /* 3rd party modules */ var hello = require('./mod_server2'); /*

    standard modules */ var http = require('http'), assert = require('assert'); /* config */ var PORT = 9000, options = { host: 'localhost', port: PORT, path: '/', method: 'GET' }; /* functions */ var logm = function(str){ // yellow console.log('\033[33m C: ' + str + '\033[39m'); }; /* * Error log */ var loge = function(str){ // cyan console.error('\036[33m CError: ' + str + '\033[39m'); }; var http_req = function(options, callback, close){ var server_response = ''; var req = http.request(options, function(res){ assert.equal(200, res.statusCode, 'status code should be 200, but got '+res.statusCode); res.on('data', function(chunk) { server_response += chunk; }); res.on('end', function(){ if(close){ hello.server.close(); } callback(server_response); }); }); req.end(); req.on('error', function(e){ loge('an error occured: '); loge(e); }); }; /* code starts here */ hello.server.listen(PORT, function(){ // 1st test options.path = "/"; http_req(options, function(res){ assert.equal(res, "Hello World"); }); // 2nd test options.path = "/hello"; http_req(options, function(res){ assert.equal(res, "Hello Hello"); }); // last test options.path = "/last"; http_req(options, function(res){ assert.equal(res, "Bye!"); }, 'close'); }); /* assert response */ process.on('exit', function(){ console.log('\033[7m test passed \033[27m'); }); var app = exports.server = require('express').createServer(); app.get('/', function(req, res){ res.send('Hello World'); }); app.get('/hello', function(req, res){ res.send('Hello Hello'); }); app.get('/last', function(req, res){ res.send('Bye!'); }); ֦ுͰύϫϑϧʹ ςετ͔Βॻ͍͍ͯ͘ͱָͪΜ
  70. ͋ͱ͸Θ͔Δͳʁ Thanks!

  71. http://nodejs.jp/ ಡΜͩޙʹ/PEFKT Ϣʔβάϧʔϓͷ αΠτʹΞΫηε͢Δ ·Ͱ͕ೖ໳ฤͩͬʂ