SlideShare une entreprise Scribd logo
1  sur  48
Télécharger pour lire hors ligne
Developing and Testing a MongoDB
and Node.js REST API
Valeri Karpov
Node.js Engineer, MongoDB
www.thecodebarbarian.com
github.com/vkarpov15
@code_barbarian
*
What is this talk about?
•Briefly introduce MongoDB and Node.js
•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
•Upcoming EdX course
*
What is MongoDB?
•Document database - store objects, not columns
•Often better* performance
• Better data locality than RDBMS for 1 query vs 5 JOINs
*
What is MongoDB?
•Easier ops
• No need to set up databases, tables, schemas, etc.
• Run one executable, you now have a working db
• MongoDB Cloud Manager and Ops Manager
•Developer sanity
• In my experience, easiest database to get working
• Minimal data transformation between DB and server
• Company has dedicated “Developer Experience” team
*
Overview
•Part 1: Shopping Cart Application
• Search for products
• Add them to your cart
• Check out with Stripe
•Part 2: Using Node.js and the Mongoose ODM
•Part 3: Schema Design
•Part 4: Building an API with the Express framework
•Part 5: Testing with Mocha + Superagent
*
Part 1: 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
•Representing relationships in MongoDB is tricky
• MongoDB doesn’t have a JOIN equivalent
•But that’s what mongoose is for
• Mongoose provides pseudo-JOINs (separate queries)
*
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.
*
Brief Overview of Node.js
•require()
● Async I/O
*
What Does Async Mean?
Register event
handler
In node.js, you don’t execute I/O imperatively.
You register a callback to execute when the I/O is done
Single thread - handler can’t be interrupted
Prints before
“done reading”
*
Why Async?
•Most web app/mobile app backends are I/O bound
• Multi-threading doesn’t help if CPU is just waiting for I/O
• Mostly just waiting for database to respond
•Nowadays there’s a REST API for everything
• Analytics and tracking: Segment
• Transactional email: Vero
• Mailing lists: MailChimp
•Threads are cumbersome for I/O bound programs
*
Your First Mongoose Schema
matches X@Y.Z
*
Using Your Schema
Create User
Save user to MongoDB
Load user from MongoDB
Print user to stdout
*
Part 2 Takeaways
•Mongoose provides several neat features
• Model part of MVC
• Default values
• Schema validation and declarative schema design
*
Part 3: Schema Design
•3 schemas:
• Product
• Category
• User
•Going to use mongoose to define schemas
•Will use a couple key schema design principles
*
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
•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
*
User Schema
User’s cart as an array
of ObjectIds...
*
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
*
Part 4: The Express Framework
•Most popular Node.js web framework
•Simple, pluggable, and fast
•Great tool for building REST APIs
*
Your First Express App
Route Parameter
*
What is REST?
•Representational State Transfer
•HTTP request -> JSON HTTP response
•Business logic on top of MongoDB schemas
• Access control, emails, analytics, etc.
*
Structuring Your REST API
*
GET /category/id/:id
Find Category
Error handling
Output JSON
*
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
Get cart from HTTP request
Overwrite user’s cart
Let mongoose handle
casting and validating data
*
PUT /me/cart Takeaways
•Mongoose lets you be lazy
•Access control using subdocs
*
Bonus: Stripe Checkout
Create a Stripe charge with the npm ‘stripe’ module
*
Bonus: Stripe Checkout
Error handling
Empty user cart
on success
*
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
•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
*
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
• describe() -> test suite
• it() -> individual test
*
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
•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!
TDD a REST API With Node.js and MongoDB

Contenu connexe

Tendances

10 tips to make your ASP.NET Apps Faster
10 tips to make your ASP.NET Apps Faster10 tips to make your ASP.NET Apps Faster
10 tips to make your ASP.NET Apps FasterBrij Mishra
 
Getting Started with ASP.NET 5
Getting Started with ASP.NET 5Getting Started with ASP.NET 5
Getting Started with ASP.NET 5Brij Mishra
 
MEAN Stack - Google Developers Live 10/03/2013
MEAN Stack - Google Developers Live 10/03/2013MEAN Stack - Google Developers Live 10/03/2013
MEAN Stack - Google Developers Live 10/03/2013Valeri Karpov
 
[Blibli Brown Bag] Nodejs - The Other Side of Javascript
[Blibli Brown Bag] Nodejs - The Other Side of Javascript[Blibli Brown Bag] Nodejs - The Other Side of Javascript
[Blibli Brown Bag] Nodejs - The Other Side of JavascriptIrfan Maulana
 
Testing your Single Page Application
Testing your Single Page ApplicationTesting your Single Page Application
Testing your Single Page ApplicationWekoslav Stefanovski
 
Journey To The Front End World - Part3 - The Machine
Journey To The Front End World - Part3 - The MachineJourney To The Front End World - Part3 - The Machine
Journey To The Front End World - Part3 - The MachineIrfan Maulana
 
Isomorphic web application
Isomorphic web applicationIsomorphic web application
Isomorphic web applicationOliver N
 
The Dark Side of Single Page Applications
The Dark Side of Single Page ApplicationsThe Dark Side of Single Page Applications
The Dark Side of Single Page ApplicationsDor Kalev
 
PHP Indonesia - Nodejs Web Development
PHP Indonesia - Nodejs Web DevelopmentPHP Indonesia - Nodejs Web Development
PHP Indonesia - Nodejs Web DevelopmentIrfan Maulana
 
Afrimadoni the power of docker
Afrimadoni   the power of dockerAfrimadoni   the power of docker
Afrimadoni the power of dockerPHP Indonesia
 
SenchaCon 2016: Being Productive with the New Sencha Fiddle - Mitchell Simoens
SenchaCon 2016: Being Productive with the New Sencha Fiddle - Mitchell Simoens  SenchaCon 2016: Being Productive with the New Sencha Fiddle - Mitchell Simoens
SenchaCon 2016: Being Productive with the New Sencha Fiddle - Mitchell Simoens Sencha
 
AngularJS vs React JS vs Node JS: Which is Best For Web Development ?
AngularJS vs React JS vs Node JS: Which is Best For Web Development ?AngularJS vs React JS vs Node JS: Which is Best For Web Development ?
AngularJS vs React JS vs Node JS: Which is Best For Web Development ?MarkupBox
 
From MEAN to the MERN Stack
From MEAN to the MERN StackFrom MEAN to the MERN Stack
From MEAN to the MERN StackTroy Miles
 

Tendances (20)

10 tips to make your ASP.NET Apps Faster
10 tips to make your ASP.NET Apps Faster10 tips to make your ASP.NET Apps Faster
10 tips to make your ASP.NET Apps Faster
 
Getting Started with ASP.NET 5
Getting Started with ASP.NET 5Getting Started with ASP.NET 5
Getting Started with ASP.NET 5
 
ITT Flisol 2013
ITT Flisol 2013ITT Flisol 2013
ITT Flisol 2013
 
MEAN Stack - Google Developers Live 10/03/2013
MEAN Stack - Google Developers Live 10/03/2013MEAN Stack - Google Developers Live 10/03/2013
MEAN Stack - Google Developers Live 10/03/2013
 
Hello world - intro to node js
Hello world - intro to node jsHello world - intro to node js
Hello world - intro to node js
 
[Blibli Brown Bag] Nodejs - The Other Side of Javascript
[Blibli Brown Bag] Nodejs - The Other Side of Javascript[Blibli Brown Bag] Nodejs - The Other Side of Javascript
[Blibli Brown Bag] Nodejs - The Other Side of Javascript
 
Testing your Single Page Application
Testing your Single Page ApplicationTesting your Single Page Application
Testing your Single Page Application
 
Journey To The Front End World - Part3 - The Machine
Journey To The Front End World - Part3 - The MachineJourney To The Front End World - Part3 - The Machine
Journey To The Front End World - Part3 - The Machine
 
Meanstack overview
Meanstack overviewMeanstack overview
Meanstack overview
 
Isomorphic web application
Isomorphic web applicationIsomorphic web application
Isomorphic web application
 
The Dark Side of Single Page Applications
The Dark Side of Single Page ApplicationsThe Dark Side of Single Page Applications
The Dark Side of Single Page Applications
 
Node js projects
Node js projectsNode js projects
Node js projects
 
PHP Indonesia - Nodejs Web Development
PHP Indonesia - Nodejs Web DevelopmentPHP Indonesia - Nodejs Web Development
PHP Indonesia - Nodejs Web Development
 
MEAN Stack
MEAN Stack MEAN Stack
MEAN Stack
 
The MEAN Stack
The MEAN StackThe MEAN Stack
The MEAN Stack
 
Afrimadoni the power of docker
Afrimadoni   the power of dockerAfrimadoni   the power of docker
Afrimadoni the power of docker
 
SenchaCon 2016: Being Productive with the New Sencha Fiddle - Mitchell Simoens
SenchaCon 2016: Being Productive with the New Sencha Fiddle - Mitchell Simoens  SenchaCon 2016: Being Productive with the New Sencha Fiddle - Mitchell Simoens
SenchaCon 2016: Being Productive with the New Sencha Fiddle - Mitchell Simoens
 
Codegen2021 blazor mobile
Codegen2021 blazor mobileCodegen2021 blazor mobile
Codegen2021 blazor mobile
 
AngularJS vs React JS vs Node JS: Which is Best For Web Development ?
AngularJS vs React JS vs Node JS: Which is Best For Web Development ?AngularJS vs React JS vs Node JS: Which is Best For Web Development ?
AngularJS vs React JS vs Node JS: Which is Best For Web Development ?
 
From MEAN to the MERN Stack
From MEAN to the MERN StackFrom MEAN to the MERN Stack
From MEAN to the MERN Stack
 

En vedette

Developing and Testing a MongoDB and Node.js REST API
Developing and Testing a MongoDB and Node.js REST APIDeveloping and Testing a MongoDB and Node.js REST API
Developing and Testing a MongoDB and Node.js REST APIAll Things Open
 
Webinar: Get Started with the MEAN Stack
Webinar: Get Started with the MEAN StackWebinar: Get Started with the MEAN Stack
Webinar: Get Started with the MEAN StackMongoDB
 
Develop a Basic REST API from Scratch Using TDD with Val Karpov
Develop a Basic REST API from Scratch Using TDD with Val KarpovDevelop a Basic REST API from Scratch Using TDD with Val Karpov
Develop a Basic REST API from Scratch Using TDD with Val KarpovMongoDB
 
Web Service Workshop - 3 days
Web Service Workshop - 3 daysWeb Service Workshop - 3 days
Web Service Workshop - 3 daysDavid Ionut
 
JS-IL Keynote: MongoDB 2.6, Mongoose 4.0, and Beyond
JS-IL Keynote: MongoDB 2.6, Mongoose 4.0, and BeyondJS-IL Keynote: MongoDB 2.6, Mongoose 4.0, and Beyond
JS-IL Keynote: MongoDB 2.6, Mongoose 4.0, and BeyondValeri Karpov
 
MongoDB Miami Meetup 1/26/15: Introduction to WiredTiger
MongoDB Miami Meetup 1/26/15: Introduction to WiredTigerMongoDB Miami Meetup 1/26/15: Introduction to WiredTiger
MongoDB Miami Meetup 1/26/15: Introduction to WiredTigerValeri Karpov
 
JS-IL: Getting MEAN in 1 Hour
JS-IL: Getting MEAN in 1 HourJS-IL: Getting MEAN in 1 Hour
JS-IL: Getting MEAN in 1 HourValeri Karpov
 
Mongo db in 3 minutes BoilerMake
Mongo db in 3 minutes   BoilerMakeMongo db in 3 minutes   BoilerMake
Mongo db in 3 minutes BoilerMakeValeri Karpov
 
MongoDB Israel June Meetup
MongoDB Israel June MeetupMongoDB Israel June Meetup
MongoDB Israel June MeetupValeri Karpov
 
AngularJS Meetup 11/19/13 - AngularJS for MongoDB Continuous Integration
AngularJS Meetup 11/19/13 - AngularJS for MongoDB Continuous IntegrationAngularJS Meetup 11/19/13 - AngularJS for MongoDB Continuous Integration
AngularJS Meetup 11/19/13 - AngularJS for MongoDB Continuous IntegrationValeri Karpov
 
MongoDB: Queries and Aggregation Framework with NBA Game Data
MongoDB: Queries and Aggregation Framework with NBA Game DataMongoDB: Queries and Aggregation Framework with NBA Game Data
MongoDB: Queries and Aggregation Framework with NBA Game DataValeri Karpov
 
NodeSummit - MEAN Stack
NodeSummit - MEAN StackNodeSummit - MEAN Stack
NodeSummit - MEAN StackValeri Karpov
 
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)Valeri Karpov
 
Postman Collection Format v2.0 (pre-draft)
Postman Collection Format v2.0 (pre-draft)Postman Collection Format v2.0 (pre-draft)
Postman Collection Format v2.0 (pre-draft)Postman
 
Securty Testing For RESTful Applications
Securty Testing For RESTful ApplicationsSecurty Testing For RESTful Applications
Securty Testing For RESTful ApplicationsSource Conference
 
TestLink introduction
TestLink introductionTestLink introduction
TestLink introductionDavid Ionut
 

En vedette (20)

Developing and Testing a MongoDB and Node.js REST API
Developing and Testing a MongoDB and Node.js REST APIDeveloping and Testing a MongoDB and Node.js REST API
Developing and Testing a MongoDB and Node.js REST API
 
Webinar: Get Started with the MEAN Stack
Webinar: Get Started with the MEAN StackWebinar: Get Started with the MEAN Stack
Webinar: Get Started with the MEAN Stack
 
Develop a Basic REST API from Scratch Using TDD with Val Karpov
Develop a Basic REST API from Scratch Using TDD with Val KarpovDevelop a Basic REST API from Scratch Using TDD with Val Karpov
Develop a Basic REST API from Scratch Using TDD with Val Karpov
 
Web Service Workshop - 3 days
Web Service Workshop - 3 daysWeb Service Workshop - 3 days
Web Service Workshop - 3 days
 
JS-IL Keynote: MongoDB 2.6, Mongoose 4.0, and Beyond
JS-IL Keynote: MongoDB 2.6, Mongoose 4.0, and BeyondJS-IL Keynote: MongoDB 2.6, Mongoose 4.0, and Beyond
JS-IL Keynote: MongoDB 2.6, Mongoose 4.0, and Beyond
 
MongoDB Miami Meetup 1/26/15: Introduction to WiredTiger
MongoDB Miami Meetup 1/26/15: Introduction to WiredTigerMongoDB Miami Meetup 1/26/15: Introduction to WiredTiger
MongoDB Miami Meetup 1/26/15: Introduction to WiredTiger
 
JS-IL: Getting MEAN in 1 Hour
JS-IL: Getting MEAN in 1 HourJS-IL: Getting MEAN in 1 Hour
JS-IL: Getting MEAN in 1 Hour
 
Mongo db in 3 minutes BoilerMake
Mongo db in 3 minutes   BoilerMakeMongo db in 3 minutes   BoilerMake
Mongo db in 3 minutes BoilerMake
 
MongoDB Israel June Meetup
MongoDB Israel June MeetupMongoDB Israel June Meetup
MongoDB Israel June Meetup
 
AngularJS Meetup 11/19/13 - AngularJS for MongoDB Continuous Integration
AngularJS Meetup 11/19/13 - AngularJS for MongoDB Continuous IntegrationAngularJS Meetup 11/19/13 - AngularJS for MongoDB Continuous Integration
AngularJS Meetup 11/19/13 - AngularJS for MongoDB Continuous Integration
 
JSON Injection
JSON InjectionJSON Injection
JSON Injection
 
MongoDB: Queries and Aggregation Framework with NBA Game Data
MongoDB: Queries and Aggregation Framework with NBA Game DataMongoDB: Queries and Aggregation Framework with NBA Game Data
MongoDB: Queries and Aggregation Framework with NBA Game Data
 
NodeSummit - MEAN Stack
NodeSummit - MEAN StackNodeSummit - MEAN Stack
NodeSummit - MEAN Stack
 
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)
 
Postman Collection Format v2.0 (pre-draft)
Postman Collection Format v2.0 (pre-draft)Postman Collection Format v2.0 (pre-draft)
Postman Collection Format v2.0 (pre-draft)
 
Pentesting RESTful WebServices v1.0
Pentesting RESTful WebServices v1.0Pentesting RESTful WebServices v1.0
Pentesting RESTful WebServices v1.0
 
Securty Testing For RESTful Applications
Securty Testing For RESTful ApplicationsSecurty Testing For RESTful Applications
Securty Testing For RESTful Applications
 
API Testing
API TestingAPI Testing
API Testing
 
Api testing
Api testingApi testing
Api testing
 
TestLink introduction
TestLink introductionTestLink introduction
TestLink introduction
 

Similaire à TDD a REST API With Node.js and MongoDB

Escaping the yellow bubble - rewriting Domino using MongoDb and Angular
Escaping the yellow bubble - rewriting Domino using MongoDb and AngularEscaping the yellow bubble - rewriting Domino using MongoDb and Angular
Escaping the yellow bubble - rewriting Domino using MongoDb and AngularMark Leusink
 
NodeJS - Server Side JS
NodeJS - Server Side JS NodeJS - Server Side JS
NodeJS - Server Side JS Ganesh Kondal
 
Cloud App Develop
Cloud App DevelopCloud App Develop
Cloud App DevelopFin Chen
 
MongoDB at Gilt Groupe
MongoDB at Gilt GroupeMongoDB at Gilt Groupe
MongoDB at Gilt GroupeMongoDB
 
An introduction to Node.js
An introduction to Node.jsAn introduction to Node.js
An introduction to Node.jsKasey McCurdy
 
When to Use MongoDB
When to Use MongoDBWhen to Use MongoDB
When to Use MongoDBMongoDB
 
Mongodb at-gilt-groupe-seattle-2012-09-14-final
Mongodb at-gilt-groupe-seattle-2012-09-14-finalMongodb at-gilt-groupe-seattle-2012-09-14-final
Mongodb at-gilt-groupe-seattle-2012-09-14-finalMongoDB
 
Tech io nodejs_20130531_v0.6
Tech io nodejs_20130531_v0.6Tech io nodejs_20130531_v0.6
Tech io nodejs_20130531_v0.6Ganesh Kondal
 
Mongo db first steps with csharp
Mongo db first steps with csharpMongo db first steps with csharp
Mongo db first steps with csharpSerdar Buyuktemiz
 
Surrogate dependencies (in node js) v1.0
Surrogate dependencies  (in node js)  v1.0Surrogate dependencies  (in node js)  v1.0
Surrogate dependencies (in node js) v1.0Dinis Cruz
 
What is Mean Stack Development ?
What is Mean Stack Development ?What is Mean Stack Development ?
What is Mean Stack Development ?Balajihope
 
Tech io spa_angularjs_20130814_v0.9.5
Tech io spa_angularjs_20130814_v0.9.5Tech io spa_angularjs_20130814_v0.9.5
Tech io spa_angularjs_20130814_v0.9.5Ganesh Kondal
 
Mongo DB at Community Engine
Mongo DB at Community EngineMongo DB at Community Engine
Mongo DB at Community EngineCommunity Engine
 
MongoDB at community engine
MongoDB at community engineMongoDB at community engine
MongoDB at community enginemathraq
 

Similaire à TDD a REST API With Node.js and MongoDB (20)

Escaping the yellow bubble - rewriting Domino using MongoDb and Angular
Escaping the yellow bubble - rewriting Domino using MongoDb and AngularEscaping the yellow bubble - rewriting Domino using MongoDb and Angular
Escaping the yellow bubble - rewriting Domino using MongoDb and Angular
 
20120306 dublin js
20120306 dublin js20120306 dublin js
20120306 dublin js
 
NodeJS - Server Side JS
NodeJS - Server Side JS NodeJS - Server Side JS
NodeJS - Server Side JS
 
Cloud App Develop
Cloud App DevelopCloud App Develop
Cloud App Develop
 
Beginners Node.js
Beginners Node.jsBeginners Node.js
Beginners Node.js
 
Mean stack
Mean stackMean stack
Mean stack
 
MongoDB at Gilt Groupe
MongoDB at Gilt GroupeMongoDB at Gilt Groupe
MongoDB at Gilt Groupe
 
MongoDB
MongoDBMongoDB
MongoDB
 
An introduction to Node.js
An introduction to Node.jsAn introduction to Node.js
An introduction to Node.js
 
When to Use MongoDB
When to Use MongoDBWhen to Use MongoDB
When to Use MongoDB
 
Mongodb at-gilt-groupe-seattle-2012-09-14-final
Mongodb at-gilt-groupe-seattle-2012-09-14-finalMongodb at-gilt-groupe-seattle-2012-09-14-final
Mongodb at-gilt-groupe-seattle-2012-09-14-final
 
Tech io nodejs_20130531_v0.6
Tech io nodejs_20130531_v0.6Tech io nodejs_20130531_v0.6
Tech io nodejs_20130531_v0.6
 
Mongo db first steps with csharp
Mongo db first steps with csharpMongo db first steps with csharp
Mongo db first steps with csharp
 
Node js for enterprise
Node js for enterpriseNode js for enterprise
Node js for enterprise
 
Surrogate dependencies (in node js) v1.0
Surrogate dependencies  (in node js)  v1.0Surrogate dependencies  (in node js)  v1.0
Surrogate dependencies (in node js) v1.0
 
What is Mean Stack Development ?
What is Mean Stack Development ?What is Mean Stack Development ?
What is Mean Stack Development ?
 
Tech io spa_angularjs_20130814_v0.9.5
Tech io spa_angularjs_20130814_v0.9.5Tech io spa_angularjs_20130814_v0.9.5
Tech io spa_angularjs_20130814_v0.9.5
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Mongo DB at Community Engine
Mongo DB at Community EngineMongo DB at Community Engine
Mongo DB at Community Engine
 
MongoDB at community engine
MongoDB at community engineMongoDB at community engine
MongoDB at community engine
 

Plus de Valeri Karpov

A Practical Introduction to GeoJSON
A Practical Introduction to GeoJSONA Practical Introduction to GeoJSON
A Practical Introduction to GeoJSONValeri Karpov
 
A Practical Introduction to Functions-as-a-Service
A Practical Introduction to Functions-as-a-ServiceA Practical Introduction to Functions-as-a-Service
A Practical Introduction to Functions-as-a-ServiceValeri Karpov
 
A Gentle Introduction to Functions-as-a-Service
A Gentle Introduction to Functions-as-a-ServiceA Gentle Introduction to Functions-as-a-Service
A Gentle Introduction to Functions-as-a-ServiceValeri Karpov
 
Introducing Async/Await
Introducing Async/AwaitIntroducing Async/Await
Introducing Async/AwaitValeri Karpov
 
TAO and the Essence of Modern JavaScript
TAO and the Essence of Modern JavaScriptTAO and the Essence of Modern JavaScript
TAO and the Essence of Modern JavaScriptValeri Karpov
 
Mastering Async/Await in JavaScript
Mastering Async/Await in JavaScriptMastering Async/Await in JavaScript
Mastering Async/Await in JavaScriptValeri Karpov
 
React, Redux, and Archetype
React, Redux, and ArchetypeReact, Redux, and Archetype
React, Redux, and ArchetypeValeri Karpov
 
MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQ...
MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQ...MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQ...
MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQ...Valeri Karpov
 
MongoDB API Talk @ HackPrinceton
MongoDB API Talk @ HackPrincetonMongoDB API Talk @ HackPrinceton
MongoDB API Talk @ HackPrincetonValeri Karpov
 

Plus de Valeri Karpov (9)

A Practical Introduction to GeoJSON
A Practical Introduction to GeoJSONA Practical Introduction to GeoJSON
A Practical Introduction to GeoJSON
 
A Practical Introduction to Functions-as-a-Service
A Practical Introduction to Functions-as-a-ServiceA Practical Introduction to Functions-as-a-Service
A Practical Introduction to Functions-as-a-Service
 
A Gentle Introduction to Functions-as-a-Service
A Gentle Introduction to Functions-as-a-ServiceA Gentle Introduction to Functions-as-a-Service
A Gentle Introduction to Functions-as-a-Service
 
Introducing Async/Await
Introducing Async/AwaitIntroducing Async/Await
Introducing Async/Await
 
TAO and the Essence of Modern JavaScript
TAO and the Essence of Modern JavaScriptTAO and the Essence of Modern JavaScript
TAO and the Essence of Modern JavaScript
 
Mastering Async/Await in JavaScript
Mastering Async/Await in JavaScriptMastering Async/Await in JavaScript
Mastering Async/Await in JavaScript
 
React, Redux, and Archetype
React, Redux, and ArchetypeReact, Redux, and Archetype
React, Redux, and Archetype
 
MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQ...
MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQ...MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQ...
MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQ...
 
MongoDB API Talk @ HackPrinceton
MongoDB API Talk @ HackPrincetonMongoDB API Talk @ HackPrinceton
MongoDB API Talk @ HackPrinceton
 

Dernier

Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 

Dernier (20)

E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 

TDD a REST API With Node.js and MongoDB

  • 1. Developing and Testing a MongoDB and Node.js REST API Valeri Karpov Node.js Engineer, MongoDB www.thecodebarbarian.com github.com/vkarpov15 @code_barbarian
  • 2. * What is this talk about? •Briefly introduce MongoDB and Node.js •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 •Upcoming EdX course
  • 3. * What is MongoDB? •Document database - store objects, not columns •Often better* performance • Better data locality than RDBMS for 1 query vs 5 JOINs
  • 4. * What is MongoDB? •Easier ops • No need to set up databases, tables, schemas, etc. • Run one executable, you now have a working db • MongoDB Cloud Manager and Ops Manager •Developer sanity • In my experience, easiest database to get working • Minimal data transformation between DB and server • Company has dedicated “Developer Experience” team
  • 5. * Overview •Part 1: Shopping Cart Application • Search for products • Add them to your cart • Check out with Stripe •Part 2: Using Node.js and the Mongoose ODM •Part 3: Schema Design •Part 4: Building an API with the Express framework •Part 5: Testing with Mocha + Superagent
  • 6. * Part 1: What Does the App Do?
  • 7. * What Does the App Do?
  • 8. * What Does the App Do?
  • 9. * 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
  • 10. * Schema Relationships •Product belongs to one or more categories •Users can have multiple products in their cart •Representing relationships in MongoDB is tricky • MongoDB doesn’t have a JOIN equivalent •But that’s what mongoose is for • Mongoose provides pseudo-JOINs (separate queries)
  • 11. * 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.
  • 12. * Brief Overview of Node.js •require() ● Async I/O
  • 13. * What Does Async Mean? Register event handler In node.js, you don’t execute I/O imperatively. You register a callback to execute when the I/O is done Single thread - handler can’t be interrupted Prints before “done reading”
  • 14. * Why Async? •Most web app/mobile app backends are I/O bound • Multi-threading doesn’t help if CPU is just waiting for I/O • Mostly just waiting for database to respond •Nowadays there’s a REST API for everything • Analytics and tracking: Segment • Transactional email: Vero • Mailing lists: MailChimp •Threads are cumbersome for I/O bound programs
  • 15. * Your First Mongoose Schema matches X@Y.Z
  • 16. * Using Your Schema Create User Save user to MongoDB Load user from MongoDB Print user to stdout
  • 17. * Part 2 Takeaways •Mongoose provides several neat features • Model part of MVC • Default values • Schema validation and declarative schema design
  • 18. * Part 3: Schema Design •3 schemas: • Product • Category • User •Going to use mongoose to define schemas •Will use a couple key schema design principles
  • 22. * Category Schema Queries •What categories are descendants of “Electronics”? • • •What categories are children of “Non-Fiction”? • •What categories are ancestors of “Phones”?
  • 24. * 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
  • 25. * User Schema User’s cart as an array of ObjectIds...
  • 26. * 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
  • 27. * Part 4: The Express Framework •Most popular Node.js web framework •Simple, pluggable, and fast •Great tool for building REST APIs
  • 28. * Your First Express App Route Parameter
  • 29. * What is REST? •Representational State Transfer •HTTP request -> JSON HTTP response •Business logic on top of MongoDB schemas • Access control, emails, analytics, etc.
  • 34. * Adding Products to User’s Cart •Recall cart is an array of products
  • 35. * Adding Products to User’s Cart Get cart from HTTP request Overwrite user’s cart Let mongoose handle casting and validating data
  • 36. * PUT /me/cart Takeaways •Mongoose lets you be lazy •Access control using subdocs
  • 37. * Bonus: Stripe Checkout Create a Stripe charge with the npm ‘stripe’ module
  • 38. * Bonus: Stripe Checkout Error handling Empty user cart on success
  • 39. * 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
  • 40. * Part 5: Test-Driven Development •Building an API is tricky •Lots of different error conditions •Express has a lot of magic under the hood
  • 41. * 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
  • 42. * Superagent •NodeJS HTTP client •Isomorphic: runs in both browser and NodeJS •Same author as Express
  • 43. * Mocha •Testing Framework for NodeJS •Same author as Express •BDD-style syntax • describe() -> test suite • it() -> individual test
  • 46. * 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
  • 47. * •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!