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
530
おうち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
Building Flexible Design Systems
yeseniaperezcruz
327
38k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
Product Roadmaps are Hard
iamctodd
PRO
50
11k
We Have a Design System, Now What?
morganepeng
51
7.3k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
17
2.3k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
Side Projects
sachag
452
42k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.9k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
The Cost Of JavaScript in 2023
addyosmani
46
7k
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Ͱܕ͚ͩ ΊΔͷՄೳ • ҆શੑ͓Αͼܕͱ͍͏υΩϡϝϯτ