the mean stack: mongodb, expressjs, angularjs and node.js

Post on 08-Sep-2014

41.273 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

Tips and tricks for using MongoDB and Node.js

TRANSCRIPT

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

What is The Ascot Project?

Making fashion geek-friendly

Less like this More like this

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

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

Why MongoDB?

1) It's cool2) No joins for nested data3) Flexible and simple

SQL is Annoying

MEAN Stack

Web dev framework for NodeJS

Superheroic frontend framework

Event-based concurrency environment

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

Same Language, Same Objects

{ "_id" : ObjectId("5161a58b46341f8a46000003"), "username" : "vkarpov" }

{ "_id" : "5161a58b46341f8a46000003", "username" : "vkarpov" }

{ "_id" : "5161a58b46341f8a46000003", "username" : "vkarpov" }

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:

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

How MongooseJS Works

A Basic MongooseJS Schema

How MongooseJS Works

Using the StockPrice Model

How MongooseJS Works

Sometimes being able to save anything isn't right

How MongooseJS Works

Helpful Error Messages

- Nested JSON errors summary- Can use this for very generic form validation- More on this later

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

How MongooseJS Works

Can't do multi-level population!

How MongooseJS Works

Nesting and populating MongooseJS Schemas

When can we use nested schemas?

Many-to-oneOne-to-manyOne-to-one

Many-to-many

MEAN Stack Superpowers

NodeJS is not just a Javascript web server

- Event based concurrency- Package manager similar to Rails gems- Makes multithreaded servers easy

Example: Bitcoins!

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++

Bitcoin P&L

- Enter NodeJS, where this is a back-of-napkin type problem

Bitcoin P&L

- Update frontend periodically? AngularJS makes it easy

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

Bitcoin P&L + MongoDB

Model – list of nested stock schemas

Routes – get list of stocks, add a new stock

Bitcoin P&L + MongoDB

AngularJS – generic no-reload form wrapper

Bitcoin P&L + MongoDB

Jade – A View To A Kill And the result:

Takeaways

- MEAN Stack is awesome- Sophisticated frontends- Multithreaded backends made easy- MongooseJS helps NodeJS and MongoDB play nice- Limitations on MongooseJS population

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

top related