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
520
おうちHackを取り入れた リノベーション事例
yusukeito
0
650
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
650
Isomorphic Swift
yusukeito
2
590
Swiftの値付きEnumをHackする
yusukeito
0
300
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
66
4.5k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
The Cost Of JavaScript in 2023
addyosmani
45
7k
Unsuck your backbone
ammeep
669
57k
Making the Leap to Tech Lead
cromwellryan
133
9k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Designing Experiences People Love
moore
138
23k
Making Projects Easy
brettharned
116
5.9k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
How To Stay Up To Date on Web Technology
chriscoyier
789
250k
Building an army of robots
kneath
302
44k
KATA
mclloyd
29
14k
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Ͱܕ͚ͩ ΊΔͷՄೳ • ҆શੑ͓Αͼܕͱ͍͏υΩϡϝϯτ