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
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Side Projects
sachag
452
42k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
46
2.1k
It's Worth the Effort
3n
183
27k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
The Cost Of JavaScript in 2023
addyosmani
45
6.6k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
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Ͱܕ͚ͩ ΊΔͷՄೳ • ҆શੑ͓Αͼܕͱ͍͏υΩϡϝϯτ