Save 37% off PRO during our Black Friday Sale! »

Node.js+Mongoose+MongoDBで作るWebアプリ

5287c9b9576749092d3170ec738338b8?s=47 Yusuke Ito
July 23, 2013
1.8k

 Node.js+Mongoose+MongoDBで作るWebアプリ

5287c9b9576749092d3170ec738338b8?s=128

Yusuke Ito

July 23, 2013
Tweet

Transcript

  1. Node.js+Mongoose+MongoDB Ͱ࡞ΔWebΞϓϦ Yusuke Ito, @novi_, Github: novi ؙͷ಺MongoDBษڧձ #12 in

    ೲྋ΋Μ͝ࡇΓ
  2. CaseStudy: • ϒϩάΞϓϦΛ࡞ͬͯΈΔ • +؅ཧը໘΋࡞ͬͯΈΔ • DocumentͱEmbed DocumentΛ࢖༻ • Node.js+Express+MongoDB+Mongoose

  3. σϞϓϩδΣΫτ https://github.com/novi/mongodiary

  4. Agenda • Node • Express • MongooseͱMongoDB • ิ଍(πʔϧɺӡ༻पΓ)

  5. Overview Node (Webαʔόʔ) Express ΞϓϦ ϑϨʔϜϫʔΫ MongoDB ODM Ϛούʔ OS

    ϒϥ΢β localhost: 3000
  6. /PEFͷจԽ • υΩϡϝϯτΑΓ΋ιʔείʔυor ςετΛݟΔ(࠷ۙ͸υΩϡϝϯτ΋ଟ͍) • Ͱ͖Δ͚ͩγϯϓϧʹ • API΋؆ܿʹ • ଎͘(ಈ࡞ͱ։ൃεϐʔυ)

  7. Α͍ͱ͜Ζ • γϯϓϧ • ଎͍ (ಈ࡞ͱ։ൃεϐʔυ) • ബ͍(ίʔυ΍ϥΠϒϥϦͷ૚) • σόοά΍ςετָ͕

    • ϥΠϒϥϦͷ᠘ʹϋϚΓʹ͍͘
  8. ܽ఺ • ѻ͍͕ଟগ೉͍͠(ΤϥʔॲཧͳͲ) • νʔϜͷٕज़Ϩϕϧɾ஌ࣝͷ͹Β͖ͭΛͲ͏཈͑ Δ͔ • ࿮૊Έͱͯ͠ͷϑϨʔϜϫʔΫΛϓϩδΣΫτ͝ͱ ʹ࡞Δඞཁ͕͋Δ •

    (ٯʹϓϩδΣΫτ͝ͱʹϑϨʔϜϫʔΫΛ࠷దԽ Ͱ͖Δͷ΋ັྗ) • ͦΕΛͣͬͱ΍͍ͬͯ͘ͱRailsʹ౸ୡ͢Δ...
  9. ܽ఺ • ઃܭ(API, ϑΝΠϧߏ੒, ϑΝΠϧ෼ׂ) ͷܾ·Γ͕ແ͍ • ࣗ༝͚ͩͲϓϩδΣΫτ͝ͱʹϧʔϧ ͸ඞཁ •

    ಉ͘͡ϝϯςφϯε͢ΔਓͷεΩϧ ্ख͘࢖͑͹ɺ଎͞(։ൃεϐʔυ)ͱ଎͞ (ಈ࡞଎౓)Λ֫ಘͰ͖Δ
  10. Hello World $ cat app.js console.log(‘Hello World’); $ node app

    Hello World
  11. ϞδϡʔϧΛ࢖͏ app.js var fs = require(‘fs’); fs.readFile(‘hoge.txt’, ‘utf8’, function(err, data)

    { if (err) return console.error(err); console.log(data); });
  12. 8FCαʔόʔ var http = require('http'); var srv = http.createServer( function

    (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); srv.listen(1337);
  13. Express • NodeͰWebΞϓϦΛ࡞ΔͨΊͷϑϨʔ ϜϫʔΫ • ϧʔςΟϯά+ϢʔςΟϦςΟ • node-connectϕʔε • Sinatra

    Inspired (ެࣜΑΓ)
  14. ಛ௃ • جຊϓϩάϥϜͷ্͔ΒԼʹ࣮ߦ͞Ε Δ • ྫ֎΍ϧʔϧ(Rail)͸͋·Γແ͍ • Middleware͕ϙΠϯτ

  15. Mongoose • ODM(Object-Document-Mapper) • RailsͰ͍͏ActiveRecordͷҐஔ෇͚ • MongoDB(node-mongodb-native)ͷബ͍ϥ ούʔ • εΩʔϚͱ؆୯ͳόϦσʔγϣϯ

  16. Schema-less ??? http://blog.serverdensity.com/mongodb-schema-design-pitfalls/ c.f. εΩʔϚઃܭͷམͱ݀͠ͱίπ https://twitter.com/derickr/statuses/300984145662853120

  17. εΩʔϚઃܭ • εΩʔϚઃܭ͸೉͍͠ • αʔϏεઃܭͱMongoDBͷ੍໿ͷ྆ํ ͔Βߟ͑Δඞཁ͕͋Δ • ͏·࣮͘૷Ͱ͖ΔͱߴύϑΥʔϚϯε

  18. $BTF4UVEZϒϩάΞϓϦ • ͍ΘΏΔCMS(Content Management System) • Ӿཡը໘ͱ؅ཧը໘(/admin/*) • ίϝϯτͱLikeػೳ

  19. ΦϒδΣΫτυΩϡϝϯτͷઃܭ Article Author •_id (ObjectId) •email (String) •name (String) •_id

    (ObjectId) •author (ObjectId) •title,body (String) •likeCount (Number) •comments [Array] Comment •text (String) •text (String) Comment ࿮಺͕Document Comment͕Embed Document models/*.js
  20. ϙΠϯτ • 1 هࣄ = 1 Document • Document಺ʹCommentsͱlikeCountΛ಺แ •

    Document=هࣄΛফ͢ͱComments΋ಉ࣌ʹ ফ͑Δ(ઃܭʹΑΔޮՌ) • author͸ObjectIdʹΑΔࢀর • αʔϏεͷੑ্࣭τϥϯβΫγϣϯ΍atomicੑ ͸ඞཁͳ͍ͷͰ໰୊ແ͍
  21. API • / GET • /article/:id GET • /article/:id/like POST

    • /article/:id/comments POST • /admin/* ҎԼ͸ཁϩάΠϯ • /login POST • /signup POST
  22. • /admin/article POST(new) • /admin/articles GET • /admin/article/:id GET/POST (edit)

    • /admin/comments GET
  23. Appendix

  24. ӡ༻ྫ • ϑϩϯτ: nginx (port 80) • ϩΪϯά+CDN • ϦόʔεϓϩΩγ

    • ͏͠Ζ: NodeͷAppαʔόʔ × 1·ͨ͸N
  25. nginx domain:80 js, css, png, jpeg, etc... ϦΫΤετ node localhost:3001

    /login, API, etc... ϦόʔεϓϩΩγ • ϑϩϯτ(port 80)ʹWebαʔόʔ(nginxͳͲ)Λஔ͘ • ੩తϑΝΠϧ΍Ξηοτ(HTML, JS, CSS, ը૾)͸WebαʔόʔͰฦ͢ • ϩά΋WebαʔόʔͰग़ྗ͢Δ • ͦΕҎ֎ͷಈతHTML, JSON ϕʔεͷ API, WebSocketྨ͸Nodeʹϓϩ Ωγ͢Δ • ෛՙʹԠͯ͡NodeΞϓϦΛ૿΍͢
  26. Callback ? Promise ? Fiber ? • ίʔϧόοΫ஍ࠈΛͲ͏͢Δ͔ʁ • Promise

    ͕ྑ͍͕ΤϥʔॲཧΛೖΕΔͱ݁ߏ ෳࡶʹͳΔ • synchronize(fiberͷϥούʔ)͕ྑ͍͔΋͠Ε ͳ͍(ݸਓతʹ͸ͬͪ͜Λ࢖༻) • 0.12.x͔ΒyieldʹରԠ (ωΠςΟϒͷfiber)
  27. Tools • httpie (Ϟμϯcurl) • node-dev, node-inspector (σόοΨ) • grunt

    • bower • Mocha.js • http-node-proxy
  28. ܕ • TypeScript͸༗๬ • JavaͷελΠϧʹ͍ۙ • ࠓ͸JSͰॻ͍ͯকདྷతʹt.dsͰܕ͚ͩ͸ ΊΔͷ΋Մೳ • ҆શੑ͓Αͼܕͱ͍͏υΩϡϝϯτ