Intro to the MEAN Stack
Using NodeJS to drive superheroic web apps

Valeri Karpov
Kernel Tools Engineer, MongoDB
thecodebarbarian.com
github.com/vkarpov15
@code_barbarian
Who is this guy?
CTO 2008
What does MEAN mean?
MongoDB - document database
ExpressJS - web framework for NodeJS
AngularJS - client MVVM, server ⇔ UX
NodeJS - event-driven IO in Javascript
What is this talk about?
- Building a new web app: its easy…
- … except when it isn’t
Expectation

Reality
The Problem Categories
Your problems look something like this:
Prototype. Build a usable product fast
Adapt. Iterate on feedback
Test. Automate testing using CI
Scale. Utilize server resources efficiently
Prototype
“Done is better than perfect.”
- Facebook developer mantra
Prototype
- Goal: Get usable product in users’ hands fast
- Success story: Ascot Project
Prototype
- Surface: one language makes life easier
- Surface: package management
- Deeper: tight binding from database to client
Start with a UI
Interactive client-side templating
Client-side: JSON bound to state of view
Server View of Data
Interactive client-side templating
Server receives JSON data as-is
Database View of Data
Interactive client-side templating
MongoDB stores JSON data as-is
Why is this useful?
- Lower barrier to entry for new devs
- Declarative UX
- Easier debugging
- Code reuse
Adapt
“There's a plaque on our wall that says we've
sold over 65 million albums, and I don't feel I've
accomplished anything. I feel like I'm just
getting started.”
- Eddie Van Halen
Adapt
- Application always starts out simple, but...

- REST APIs. Socket APIs. SMTP.
- How will you integrate with new tech?
The NodeJS Advantage
- Threads and locks are difficult
- Cron jobs are difficult
- Your server needs to be concurrent
- NodeJS provides easy, testable concurrency
Best Case Scenario
- Bitcoin arbitrage from apartment
- 1 hour from nothing to live trading
- 15 minutes to add a new exchange
Test
“Beware of bugs in the above code; I have only
proved it correct, not tried it.”
- Donald Knuth, former Stanford CS Professor
The Challenge
How do you prove your code works?
Success story: Bookalokal
The Evolution of Testing
- Manual testing?
- Unit tests?
- E2E tests?
- Test runners?
- Continuous Integration.
MEAN Stack Advantage
- Javascript makes unit testing very easy
- Trivial dependency injection:
- AngularJS has built-in DI
- Lots of NodeJS DI, recommend
https://npmjs.org/package/omni-di
MEAN Stack Advantage
- E2E tests: angular-scenario
Karma test-runner
- http://karma-runner.github.io
- Launch browsers, run tests on file save
- Compatibility: angular-scenario, nodeunit,
jasmine
Karma and CI
- Jenkins: http://bit.ly/1fVqoC9
- Circle CI: http://bit.ly/1dhuR4A
- Travis CI: http://bit.ly/I3ojbF
- Semaphore: http://bit.ly/IgiWFz
Scale
“Go Big or Go Extinct”
- Tagline to the 2013 film Pacific Rim
A Word of Caution...
Scalability is (mostly) a Maserati Problem

http://bit.ly/17gOvUO (Moderately NSFW)
Scale… to what?
- Difficult to judge exact number, say O(1000)
- Single machine
- 20% of mistakes 80% of new apps make
- Horror story: SCNVGR, 10/18/08
What are the key points?
- Don’t be Schlemiel the Painter
Bandwidth is scarce
- Is the browser caching static assets?
- Is your CSS and JS minified?
- Grunt everybody’s favorite minifier
- Bonus minify css/js on server start using
uglify-js.
Hard drive is slow
- Especially true on EC2 - not an actual HD!
- Is your database reading from HD?
- MongoDB: In-memory “caching” built in! :)
Don’t choke the CPU
- Is your server single-threaded?
- NodeJS: You get this for free! :)
A Common Criticism
Will MEAN be obsolete in 5 years? I hope so.

“To know ten thousand things, know one thing
well.”
- The Book of Five Rings by Miyamoto Musashi
Thanks for Listening!
Comments, questions, haikus?
Read more at:
thecodebarbarian.com
github.com/vkarpov15
@code_barbarian

NodeSummit - MEAN Stack

  • 1.
    Intro to theMEAN Stack Using NodeJS to drive superheroic web apps Valeri Karpov Kernel Tools Engineer, MongoDB thecodebarbarian.com github.com/vkarpov15 @code_barbarian
  • 2.
    Who is thisguy? CTO 2008
  • 3.
    What does MEANmean? MongoDB - document database ExpressJS - web framework for NodeJS AngularJS - client MVVM, server ⇔ UX NodeJS - event-driven IO in Javascript
  • 4.
    What is thistalk about? - Building a new web app: its easy… - … except when it isn’t Expectation Reality
  • 5.
    The Problem Categories Yourproblems look something like this: Prototype. Build a usable product fast Adapt. Iterate on feedback Test. Automate testing using CI Scale. Utilize server resources efficiently
  • 6.
    Prototype “Done is betterthan perfect.” - Facebook developer mantra
  • 7.
    Prototype - Goal: Getusable product in users’ hands fast - Success story: Ascot Project
  • 8.
    Prototype - Surface: onelanguage makes life easier - Surface: package management - Deeper: tight binding from database to client
  • 9.
    Start with aUI Interactive client-side templating Client-side: JSON bound to state of view
  • 10.
    Server View ofData Interactive client-side templating Server receives JSON data as-is
  • 11.
    Database View ofData Interactive client-side templating MongoDB stores JSON data as-is
  • 12.
    Why is thisuseful? - Lower barrier to entry for new devs - Declarative UX - Easier debugging - Code reuse
  • 13.
    Adapt “There's a plaqueon our wall that says we've sold over 65 million albums, and I don't feel I've accomplished anything. I feel like I'm just getting started.” - Eddie Van Halen
  • 14.
    Adapt - Application alwaysstarts out simple, but... - REST APIs. Socket APIs. SMTP. - How will you integrate with new tech?
  • 15.
    The NodeJS Advantage -Threads and locks are difficult - Cron jobs are difficult - Your server needs to be concurrent - NodeJS provides easy, testable concurrency
  • 16.
    Best Case Scenario -Bitcoin arbitrage from apartment - 1 hour from nothing to live trading - 15 minutes to add a new exchange
  • 17.
    Test “Beware of bugsin the above code; I have only proved it correct, not tried it.” - Donald Knuth, former Stanford CS Professor
  • 18.
    The Challenge How doyou prove your code works? Success story: Bookalokal
  • 19.
    The Evolution ofTesting - Manual testing? - Unit tests? - E2E tests? - Test runners? - Continuous Integration.
  • 20.
    MEAN Stack Advantage -Javascript makes unit testing very easy - Trivial dependency injection: - AngularJS has built-in DI - Lots of NodeJS DI, recommend https://npmjs.org/package/omni-di
  • 21.
    MEAN Stack Advantage -E2E tests: angular-scenario
  • 22.
    Karma test-runner - http://karma-runner.github.io -Launch browsers, run tests on file save - Compatibility: angular-scenario, nodeunit, jasmine
  • 23.
    Karma and CI -Jenkins: http://bit.ly/1fVqoC9 - Circle CI: http://bit.ly/1dhuR4A - Travis CI: http://bit.ly/I3ojbF - Semaphore: http://bit.ly/IgiWFz
  • 24.
    Scale “Go Big orGo Extinct” - Tagline to the 2013 film Pacific Rim
  • 25.
    A Word ofCaution... Scalability is (mostly) a Maserati Problem http://bit.ly/17gOvUO (Moderately NSFW)
  • 26.
    Scale… to what? -Difficult to judge exact number, say O(1000) - Single machine - 20% of mistakes 80% of new apps make - Horror story: SCNVGR, 10/18/08
  • 27.
    What are thekey points? - Don’t be Schlemiel the Painter
  • 28.
    Bandwidth is scarce -Is the browser caching static assets? - Is your CSS and JS minified? - Grunt everybody’s favorite minifier - Bonus minify css/js on server start using uglify-js.
  • 29.
    Hard drive isslow - Especially true on EC2 - not an actual HD! - Is your database reading from HD? - MongoDB: In-memory “caching” built in! :)
  • 30.
    Don’t choke theCPU - Is your server single-threaded? - NodeJS: You get this for free! :)
  • 31.
    A Common Criticism WillMEAN be obsolete in 5 years? I hope so. “To know ten thousand things, know one thing well.” - The Book of Five Rings by Miyamoto Musashi
  • 32.
    Thanks for Listening! Comments,questions, haikus? Read more at: thecodebarbarian.com github.com/vkarpov15 @code_barbarian