develop a basic rest api from scratch using tdd with val karpov
TRANSCRIPT
Developing a Basic REST API from Scratch Using TDD
Valeri KarpovNodeJS Engineer, MongoDBwww.thecodebarbarian.com
github.com/vkarpov15@code_barbarian
*
What is this talk about?
•Earlier you heard about:– Building a basic app– Schema design principles
•MongoDB is great for storing web/mobile app data•So let’s build a REST API using NodeJS!•+ learn a bit about test-driven dev with NodeJS
*
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
*
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
*
Schema Relationships
•Product belongs to one or more categories•Users can have multiple products in their cart
*
Part 2: Using the Mongoose ODM
•“Object document mapper” (like ORM, but for MongoDB)•Written for NodeJS•Provides schema validation
*
Part 3: Schema Design
•3 schemas:– Product– Category– User
•Going to use mongoose to define schemas
*
Category Schema Queries
•What categories are descendants of “Electronics”?•
•What categories are children of “Non-Fiction”?• •What categories are ancestors of “Phones”?
*
Category Schema Takeaways
•Queries in MongoDB should be simple•“Store what you query for”•“If you need [to use the aggregation framework in an API endpoint], you're screwed anyway, and should fix your program.” - Linus Torvalds
*
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•Arrays that grow without bound are an antipattern!
*
Part 4: The Express Framework
•Most popular NodeJS web framework•Simple, pluggable, and fast•Great tool for building REST APIs
*
What is REST?
•Representational State Transfer•HTTP request -> JSON HTTP response•Business logic on top of MongoDB schemas
*
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
*
Part 5: Test-Driven Development
•Building an API is tricky•Lots of different error conditions•Express has a lot of magic under the hood
*
NodeJS Concurrency and Testing
•NodeJS runs in an event loop•Single threaded•Can run client and server on same thread!•Test server end-to-end
*
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
*
•Upcoming EdX Video Course•Looking for beta testers! Fill out survey for info
– database.mongodb.com/community-survey•More NodeJS+MongoDB content at:
– www.thecodebarbarian.com– Twitter: @code_barbarian
Thanks for Listening!