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
370
おうちHackを取り入れた リノベーション事例
yusukeito
0
630
Server Side Swiftを しばらく運用してみた話
yusukeito
0
560
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
4 Signs Your Business is Dying
shpigford
180
21k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Code Reviewing Like a Champion
maltzj
520
39k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
BBQ
matthewcrist
85
9.3k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Site-Speed That Sticks
csswizardry
0
28
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
900
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Ͱܕ͚ͩ ΊΔͷՄೳ • ҆શੑ͓Αͼܕͱ͍͏υΩϡϝϯτ