Webinar: Get Started with the MEAN Stack
-
Upload
mongodb -
Category
Technology
-
view
12.400 -
download
0
Transcript of Webinar: Get Started with the MEAN Stack
![Page 1: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/1.jpg)
Developing a Basic REST API from Scratch Using TDD
Valeri KarpovNode.js Engineer, MongoDBwww.thecodebarbarian.com
github.com/vkarpov15@code_barbarian
![Page 2: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/2.jpg)
*
What is this talk about?
•MongoDB is great for storing web/mobile app data•So let’s build a REST API using Node.js!•+ learn a bit about test-driven dev with Node.js•+ learn two MongoDB schema design principles•Server-side only - just JSON, no HTML•No AngularJS part, focus more on MongoDB•Upcoming EdX course
![Page 3: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/3.jpg)
*
Overview
•Part 1: Shopping Cart Application–Search for products–Add them to your cart–Check out with Stripe
•Part 2: Using the Mongoose ODM•Part 3: Schema Design•Part 4: Building an API with the Express framework•Part 5: Testing with Mocha + Superagent
![Page 4: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/4.jpg)
*
Part 1: What Does the App Do?
![Page 5: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/5.jpg)
*
What Does the App Do?
![Page 6: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/6.jpg)
*
What Does the App Do?
![Page 7: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/7.jpg)
*
App Structure
•"Bad programmers worry about the code. Good programmers worry about data structures and their relationships." - Linus Torvalds•3 schemas for 3 collections:•Products•Categories•Users
![Page 8: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/8.jpg)
*
Schema Relationships
•Product belongs to one or more categories•Users can have multiple products in their cart•Representing relationships in MongoDB is tricky•But that’s what mongoose is for
![Page 9: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/9.jpg)
*
Part 2: Using the Mongoose ODM•“Object document mapper” (like ORM, but for MongoDB)•“MongoDB object modeling designed to work in an asynchronous environment”•Written for Node.js•Provides schema validation, pseudo-JOINs, etc.
![Page 10: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/10.jpg)
*
Brief Overview of Node.js
•require()
•Async I/O
![Page 11: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/11.jpg)
*
What Does Async Mean?
Register eventhandler
In node.js, you don’t execute I/O imperatively.You register a callback to execute when the I/O is done
Prints before“done reading”
![Page 13: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/13.jpg)
*
Using Your Schema
Create User
Save user to MongoDB
Load user from MongoDB
Print user to stdout
![Page 14: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/14.jpg)
*
Part 2 Takeaways
•Mongoose provides several neat features–Model part of MVC–Default values–Schema validation and declarative schema design
![Page 15: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/15.jpg)
*
Part 3: Schema Design
•3 schemas:–Product–Category–User
•Going to use mongoose to define schemas•Will use a couple key schema design principles
![Page 16: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/16.jpg)
*
Product Schema
![Page 17: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/17.jpg)
*
Product Schema in Action
![Page 18: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/18.jpg)
*
Category Schema
![Page 19: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/19.jpg)
*
Category Schema Queries
•What categories are descendants of “Electronics”?•
•What categories are children of “Non-Fiction”?• •What categories are ancestors of “Phones”?
![Page 20: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/20.jpg)
*
Product + Category Schemas
![Page 21: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/21.jpg)
*
Category Schema Takeaways
•Queries in MongoDB should be simple•Strive for minimal data transformation by server•“Store what you query for”•“If you need [the aggregation framework in a heavily used API endpoint], you're screwed anyway, and should fix your program.” - Linus Torvalds•Good for performance and developer sanity
![Page 22: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/22.jpg)
*
User Schema
User’s cart as an arrayof ObjectIds...
![Page 23: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/23.jpg)
*
Principle of Least Cardinality
•Product and user = many-to-many relationship•Don’t necessarily need a mapping table•User won’t have 1000s of products in cart•Can represent relationship as array in user since one side is small•If one side of many-to-many is bounded and/or small, it is a good candidate for embedding•Arrays that grow without bound are an antipattern!
–16mb document size limit–network overhead
![Page 24: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/24.jpg)
*
Part 4: The Express Framework
•Most popular Node.js web framework•Simple, pluggable, and fast•Great tool for building REST APIs
![Page 25: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/25.jpg)
*
Your First Express App
Route Parameter
![Page 26: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/26.jpg)
*
What is REST?
•Representational State Transfer•HTTP request -> JSON HTTP response•Business logic on top of MongoDB schemas
–Access control, emails, analytics, etc.
![Page 27: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/27.jpg)
*
Structuring Your REST API
![Page 28: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/28.jpg)
*
GET /category/id/:idFind Category
Error handling
Output JSON
![Page 29: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/29.jpg)
*
GET /category/parent/:id
![Page 30: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/30.jpg)
*
GET /product/category/:id
![Page 31: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/31.jpg)
*
Adding Products to User’s Cart
•Recall cart is an array of products
![Page 32: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/32.jpg)
*
Adding Products to User’s Cart
Get cart from HTTP request
Overwrite user’s cart
Let mongoose handlecasting and validating data
![Page 33: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/33.jpg)
*
PUT /me/cart Takeaways
•Mongoose lets you be lazy•Access control using subdocs
![Page 34: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/34.jpg)
*
Bonus: Stripe Checkout
Create a Stripe charge with the npm ‘stripe’ module
![Page 35: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/35.jpg)
*
Bonus: Stripe Checkout
Error handling
Empty user carton success
![Page 36: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/36.jpg)
*
Part 4 Takeaways
•Express REST API on top of mongoose–Access control–Business logic–Define what operations user can take on database
•Mongoose casting and validation for APIs
![Page 37: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/37.jpg)
*
Part 5: Test-Driven Development•Building an API is tricky•Lots of different error conditions•Express has a lot of magic under the hood
![Page 38: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/38.jpg)
*
NodeJS Concurrency and Testing•Node.js runs in an event loop•Single threaded•Can run client and server on same thread!
–Client sends HTTP request–Client registers a callback awaiting the result–Server’s “on HTTP request” event handler is triggered
–Server sends response, continues waiting for events
–Client’s callback gets fired•Test server end-to-end
![Page 39: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/39.jpg)
*
Superagent
•NodeJS HTTP client•Isomorphic: runs in both browser and NodeJS•Same author as Express
![Page 40: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/40.jpg)
*
Mocha
•Testing Framework for NodeJS•Same author as Express•BDD-style syntax
–describe() -> test suite–it() -> individual test
![Page 41: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/41.jpg)
*
Setting Up Category API Tests
![Page 42: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/42.jpg)
*
Testing GET /category/id/:id
![Page 43: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/43.jpg)
*
Part 5 Takeaways
•NodeJS concurrency makes testing easy•Not just unit tests - full E2E for your REST API•Can manipulate database and make arbitrary HTTP requests
![Page 44: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/44.jpg)
*
•Upcoming EdX Video Course•Slides on http://www.slideshare.net/vkarpov15•Looking for beta testers! Sign up for notifications
–http://goo.gl/forms/0ckaJ4YvJN•Interested in learning about AngularJS?
–Professional AngularJS on Amazon•More NodeJS+MongoDB content at:
–www.thecodebarbarian.com–Twitter: @code_barbarian
Thanks for Listening!
![Page 45: Webinar: Get Started with the MEAN Stack](https://reader031.fdocuments.in/reader031/viewer/2022021920/586f6ef51a28ab10258b45c7/html5/thumbnails/45.jpg)