Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

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

Yusuke Ito
July 23, 2013
1.8k

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

Yusuke Ito

July 23, 2013
Tweet

Transcript

  1. ܽ఺ • ઃܭ(API, ϑΝΠϧߏ੒, ϑΝΠϧ෼ׂ) ͷܾ·Γ͕ແ͍ • ࣗ༝͚ͩͲϓϩδΣΫτ͝ͱʹϧʔϧ ͸ඞཁ •

    ಉ͘͡ϝϯςφϯε͢ΔਓͷεΩϧ ্ख͘࢖͑͹ɺ଎͞(։ൃεϐʔυ)ͱ଎͞ (ಈ࡞଎౓)Λ֫ಘͰ͖Δ
  2. 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);
  3. ΦϒδΣΫτυΩϡϝϯτͷઃܭ 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
  4. ϙΠϯτ • 1 هࣄ = 1 Document • Document಺ʹCommentsͱlikeCountΛ಺แ •

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

    • /article/:id/comments POST • /admin/* ҎԼ͸ཁϩάΠϯ • /login POST • /signup POST
  6. 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ΞϓϦΛ૿΍͢
  7. Callback ? Promise ? Fiber ? • ίʔϧόοΫ஍ࠈΛͲ͏͢Δ͔ʁ • Promise

    ͕ྑ͍͕ΤϥʔॲཧΛೖΕΔͱ݁ߏ ෳࡶʹͳΔ • synchronize(fiberͷϥούʔ)͕ྑ͍͔΋͠Ε ͳ͍(ݸਓతʹ͸ͬͪ͜Λ࢖༻) • 0.12.x͔ΒyieldʹରԠ (ωΠςΟϒͷfiber)