Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Node.js+Mongoose+MongoDBで作るWebアプリ
Search
Yusuke Ito
July 23, 2013
2
1.8k
Node.js+Mongoose+MongoDBで作るWebアプリ
Yusuke Ito
July 23, 2013
Tweet
Share
More Decks by Yusuke Ito
See All by Yusuke Ito
Macとオーディオ再生 2024/11/02
yusukeito
0
150
おうちHackを取り入れた リノベーション事例
yusukeito
0
620
Server Side Swiftを しばらく運用してみた話
yusukeito
0
550
Swift からword2vecを 使ってみる
yusukeito
0
1k
Swift Outside the Box
yusukeito
1
2.4k
SwiftでgRPCとProtocolBuffersを使う
yusukeito
4
1.3k
Swift on Raspberry Pi でI2Cデバイスを使う
yusukeito
1
640
Isomorphic Swift
yusukeito
2
580
Swiftの値付きEnumをHackする
yusukeito
0
290
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
692
190k
Building an army of robots
kneath
302
42k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.2k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
A Tale of Four Properties
chriscoyier
156
23k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
790
Happy Clients
brianwarren
97
6.7k
Imperfection Machines: The Place of Print at Facebook
scottboms
264
13k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Facilitating Awesome Meetings
lara
49
6k
The Power of CSS Pseudo Elements
geoffreycrofte
72
5.3k
Transcript
Node.js+Mongoose+MongoDB Ͱ࡞ΔWebΞϓϦ Yusuke Ito, @novi_, Github: novi ؙͷMongoDBษڧձ #12 in
ೲྋΜ͝ࡇΓ
CaseStudy: • ϒϩάΞϓϦΛ࡞ͬͯΈΔ • +ཧը໘࡞ͬͯΈΔ • DocumentͱEmbed DocumentΛ༻ • Node.js+Express+MongoDB+Mongoose
σϞϓϩδΣΫτ https://github.com/novi/mongodiary
Agenda • Node • Express • MongooseͱMongoDB • ิ(πʔϧɺӡ༻पΓ)
Overview Node (Webαʔόʔ) Express ΞϓϦ ϑϨʔϜϫʔΫ MongoDB ODM Ϛούʔ OS
ϒϥβ localhost: 3000
/PEFͷจԽ • υΩϡϝϯτΑΓιʔείʔυor ςετΛݟΔ(࠷ۙυΩϡϝϯτଟ͍) • Ͱ͖Δ͚ͩγϯϓϧʹ • API؆ܿʹ • ͘(ಈ࡞ͱ։ൃεϐʔυ)
Α͍ͱ͜Ζ • γϯϓϧ • ͍ (ಈ࡞ͱ։ൃεϐʔυ) • ബ͍(ίʔυϥΠϒϥϦͷ) • σόοάςετָ͕
• ϥΠϒϥϦͷ᠘ʹϋϚΓʹ͍͘
ܽ • ѻ͍͕ଟগ͍͠(ΤϥʔॲཧͳͲ) • νʔϜͷٕज़ϨϕϧɾࣝͷΒ͖ͭΛͲ͏͑ Δ͔ • Έͱͯ͠ͷϑϨʔϜϫʔΫΛϓϩδΣΫτ͝ͱ ʹ࡞Δඞཁ͕͋Δ •
(ٯʹϓϩδΣΫτ͝ͱʹϑϨʔϜϫʔΫΛ࠷దԽ Ͱ͖Δͷັྗ) • ͦΕΛͣͬͱ͍ͬͯ͘ͱRailsʹ౸ୡ͢Δ...
ܽ • ઃܭ(API, ϑΝΠϧߏ, ϑΝΠϧׂ) ͷܾ·Γ͕ແ͍ • ࣗ༝͚ͩͲϓϩδΣΫτ͝ͱʹϧʔϧ ඞཁ •
ಉ͘͡ϝϯςφϯε͢ΔਓͷεΩϧ ্ख͑͘ɺ͞(։ൃεϐʔυ)ͱ͞ (ಈ࡞)Λ֫ಘͰ͖Δ
Hello World $ cat app.js console.log(‘Hello World’); $ node app
Hello World
ϞδϡʔϧΛ͏ app.js var fs = require(‘fs’); fs.readFile(‘hoge.txt’, ‘utf8’, function(err, data)
{ if (err) return console.error(err); console.log(data); });
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);
Express • NodeͰWebΞϓϦΛ࡞ΔͨΊͷϑϨʔ ϜϫʔΫ • ϧʔςΟϯά+ϢʔςΟϦςΟ • node-connectϕʔε • Sinatra
Inspired (ެࣜΑΓ)
ಛ • جຊϓϩάϥϜͷ্͔ΒԼʹ࣮ߦ͞Ε Δ • ྫ֎ϧʔϧ(Rail)͋·Γແ͍ • Middleware͕ϙΠϯτ
Mongoose • ODM(Object-Document-Mapper) • RailsͰ͍͏ActiveRecordͷҐஔ͚ • MongoDB(node-mongodb-native)ͷബ͍ϥ ούʔ • εΩʔϚͱ؆୯ͳόϦσʔγϣϯ
Schema-less ??? http://blog.serverdensity.com/mongodb-schema-design-pitfalls/ c.f. εΩʔϚઃܭͷམͱ݀͠ͱίπ https://twitter.com/derickr/statuses/300984145662853120
εΩʔϚઃܭ • εΩʔϚઃܭ͍͠ • αʔϏεઃܭͱMongoDBͷ੍ͷ྆ํ ͔Βߟ͑Δඞཁ͕͋Δ • ͏·࣮͘Ͱ͖ΔͱߴύϑΥʔϚϯε
$BTF4UVEZϒϩάΞϓϦ • ͍ΘΏΔCMS(Content Management System) • Ӿཡը໘ͱཧը໘(/admin/*) • ίϝϯτͱLikeػೳ
ΦϒδΣΫτυΩϡϝϯτͷઃܭ 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
ϙΠϯτ • 1 هࣄ = 1 Document • DocumentʹCommentsͱlikeCountΛแ •
Document=هࣄΛফ͢ͱCommentsಉ࣌ʹ ফ͑Δ(ઃܭʹΑΔޮՌ) • authorObjectIdʹΑΔࢀর • αʔϏεͷੑ্࣭τϥϯβΫγϣϯatomicੑ ඞཁͳ͍ͷͰແ͍
API • / GET • /article/:id GET • /article/:id/like POST
• /article/:id/comments POST • /admin/* ҎԼཁϩάΠϯ • /login POST • /signup POST
• /admin/article POST(new) • /admin/articles GET • /admin/article/:id GET/POST (edit)
• /admin/comments GET
Appendix
ӡ༻ྫ • ϑϩϯτ: nginx (port 80) • ϩΪϯά+CDN • ϦόʔεϓϩΩγ
• ͏͠Ζ: NodeͷAppαʔόʔ × 1·ͨN
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ΞϓϦΛ૿͢
Callback ? Promise ? Fiber ? • ίʔϧόοΫࠈΛͲ͏͢Δ͔ʁ • Promise
͕ྑ͍͕ΤϥʔॲཧΛೖΕΔͱ݁ߏ ෳࡶʹͳΔ • synchronize(fiberͷϥούʔ)͕ྑ͍͔͠Ε ͳ͍(ݸਓతʹͬͪ͜Λ༻) • 0.12.x͔ΒyieldʹରԠ (ωΠςΟϒͷfiber)
Tools • httpie (Ϟμϯcurl) • node-dev, node-inspector (σόοΨ) • grunt
• bower • Mocha.js • http-node-proxy
ܕ • TypeScript༗ • JavaͷελΠϧʹ͍ۙ • ࠓJSͰॻ͍ͯকདྷతʹt.dsͰܕ͚ͩ ΊΔͷՄೳ • ҆શੑ͓Αͼܕͱ͍͏υΩϡϝϯτ