The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js
-
Upload
mongodb -
Category
Technology
-
view
41.273 -
download
3
description
Transcript of The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js
![Page 1: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/1.jpg)
A Guide to the MEAN Stack
Tips and Tricks for using MongoDB and NodeJS in overwhelmingly awesome ways
Valeri KarpovCTO, Ascot Project
www.thecodebarbarian.com@code_barbarian
github.com/vkarpov15
![Page 2: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/2.jpg)
What is The Ascot Project?
Making fashion geek-friendly
Less like this More like this
![Page 3: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/3.jpg)
Who is this guy?
- Academy for Telecommunications and Computer Science '07, Princeton CS '11- Co-founder and CTO, SCVNGR in '08- Google intern '09, mentored by Misko Hevery- Tower Research Capital '11 – '13- Developed Ascot Project at AngelHack DC in '12
![Page 4: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/4.jpg)
What is this all about?
- Why Ascot uses MongoDB- What's the MEAN stack and why its awesome- How MongoDB fits in the MEAN stack- Why MongooseJS is important and tradeoffs in it's usage- Example of a cool MEAN stack web app
![Page 5: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/5.jpg)
Why MongoDB?
1) It's cool2) No joins for nested data3) Flexible and simple
![Page 6: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/6.jpg)
SQL is Annoying
![Page 7: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/7.jpg)
MEAN Stack
Web dev framework for NodeJS
Superheroic frontend framework
Event-based concurrency environment
![Page 8: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/8.jpg)
MEAN Stack – Why do I care?
- Write one language- With MongoDB and Mongoose, easy and flexible data validation- With NodeJS, never need threads- With AngularJS, dynamic client-side templates
![Page 9: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/9.jpg)
Same Language, Same Objects
{ "_id" : ObjectId("5161a58b46341f8a46000003"), "username" : "vkarpov" }
{ "_id" : "5161a58b46341f8a46000003", "username" : "vkarpov" }
{ "_id" : "5161a58b46341f8a46000003", "username" : "vkarpov" }
![Page 10: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/10.jpg)
How MongooseJS Works
MongooseJS – code-defined schemas for MongoDB + NodeJS
- MongoDB and NodeJS are best friends
- No built-in schemas or validation
- But, MongoDB native wrapper for NodeJS is lacking:
![Page 11: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/11.jpg)
How MongooseJS Works
MongooseJS – code-defined schemas for MongoDB + NodeJS
Primary MongooseJS types:- Schema – structure defining how documents look- Connection – Wrapper around database connection- Model = Schema + Connection + collection name- Document is an instantiation of a Model
![Page 12: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/12.jpg)
How MongooseJS Works
A Basic MongooseJS Schema
![Page 13: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/13.jpg)
How MongooseJS Works
Using the StockPrice Model
![Page 14: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/14.jpg)
How MongooseJS Works
Sometimes being able to save anything isn't right
![Page 15: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/15.jpg)
How MongooseJS Works
Helpful Error Messages
- Nested JSON errors summary- Can use this for very generic form validation- More on this later
![Page 16: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/16.jpg)
How MongooseJS Works
Nesting and populating MongooseJS Schemas – an important distinction that doesn't exist in SQL
Populate – similar to SQL joinNest – re-use schema in another schema
![Page 17: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/17.jpg)
How MongooseJS Works
Can't do multi-level population!
![Page 18: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/18.jpg)
How MongooseJS Works
Nesting and populating MongooseJS Schemas
When can we use nested schemas?
Many-to-oneOne-to-manyOne-to-one
Many-to-many
![Page 19: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/19.jpg)
MEAN Stack Superpowers
NodeJS is not just a Javascript web server
- Event based concurrency- Package manager similar to Rails gems- Makes multithreaded servers easy
![Page 20: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/20.jpg)
Example: Bitcoins!
![Page 21: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/21.jpg)
Bitcoin P&L
Step 1 : Web app to display a streaming Bitcoin ticker- Complex problem – multiple threads, sockets, mutex, etc.- People in HFT get paid crazy money to do this in C++
![Page 22: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/22.jpg)
Bitcoin P&L
- Enter NodeJS, where this is a back-of-napkin type problem
![Page 23: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/23.jpg)
Bitcoin P&L
- Update frontend periodically? AngularJS makes it easy
![Page 24: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/24.jpg)
Bitcoin P&L
Takeaways
- General problem : your server connecting to other servers- Example : Ascot links- Can write multi-threaded servers, but why?
- General problem : updating page without reload- AngularJS two-way data binding
![Page 25: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/25.jpg)
Bitcoin P&L + MongoDB
Model – list of nested stock schemas
Routes – get list of stocks, add a new stock
![Page 26: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/26.jpg)
Bitcoin P&L + MongoDB
AngularJS – generic no-reload form wrapper
![Page 27: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/27.jpg)
Bitcoin P&L + MongoDB
Jade – A View To A Kill And the result:
![Page 28: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/28.jpg)
Takeaways
- MEAN Stack is awesome- Sophisticated frontends- Multithreaded backends made easy- MongooseJS helps NodeJS and MongoDB play nice- Limitations on MongooseJS population
![Page 29: The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js](https://reader030.fdocuments.us/reader030/viewer/2022012916/540e36658d7f728d7e8b4c15/html5/thumbnails/29.jpg)
Thanks for Listening!
Further Reading
- Ascot Project : www.ascotproject.com- AngularJS Form Validation : “How to Easily Validate Any Form Ever Using AngularJS” @ www.thecodebarbarian.com- SocketIO Bitcoin Price Streaming : mtgox-socket-client on NPM and Github- Bitcoin realtime P&L : github.com/vkarpov15/bitcoin-pnl, coming soon to www.thecodebarbarian.com