Developing a Basic REST API from
Scratch Using TDD
Valeri Karpov
NodeJS Engineer, MongoDB
www.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
*
What Does the App Do?
*
What Does the App Do?
*
What Does the App Do?
*
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
*
Brief Overview of NodeJS
•require()
● Async I/O
*
Your First Mongoose Schema
*
Using Your Schema
*
Using Your Schema
*
Part 3: Schema Design
•3 schemas:
– Product
– Category
– User
•Going to use mongoose to define schemas
*
Product Schema
*
Product Schema in Action
*
Category Schema
*
Category Schema Queries
•What categories are descendants of “Electronics”?
•
•What categories are children of “Non-Fiction”?
•
•What categories are ancestors of “Phones”?
*
Product + Category Schemas
*
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
*
User Schema
*
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
*
Your First Express App
*
Your First Express App
*
Your First Express App
*
What is REST?
•Representational State Transfer
•HTTP request -> JSON HTTP response
•Business logic on top of MongoDB schemas
*
Structuring Your REST API
*
GET /category/id/:id
*
GET /category/parent/:id
*
GET /product/category/:id
*
Adding Products to User’s Cart
•Recall cart is an array of products
*
Adding Products to User’s Cart
*
PUT /me/cart Takeaways
•Mongoose lets you be lazy
•Access control using subdocs
*
Bonus: Stripe Checkout
*
Bonus: Stripe Checkout
*
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
*
Superagent
•NodeJS HTTP client
•Isomorphic: runs in both browser and NodeJS
•Same author as Express
*
Mocha
•Testing Framework for NodeJS
•Same author as Express
•BDD-style syntax
*
Setting Up Category API Tests
*
Testing GET /category/id/:id
*
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!
Develop a Basic REST API from Scratch Using TDD with Val Karpov

Develop a Basic REST API from Scratch Using TDD with Val Karpov

Editor's Notes

  • #46 Customers (not just users)