SlideShare a Scribd company logo
Introduction to Node.js
–The Smiths
“When you say it's gonna happen "now"
Well when exactly do you mean?
See I've already waited too long
And all my hope is gone.”
2
3
“I hate almost all software…The only thing that matters
in software is the experience of the user. ”
-Ryan Dahl , Inventor of Node.js
http://tinyclouds.org/rant.html
1. What is Node.js?

2. How does it work?

3. What can I do with it?

4. Who is using it?

5. “Hello World!”

6. Resources Aaron Rosenberg
Mark Keith
4
Agenda
What is Node.js?
5
What is Node.js®?
• Server-side JavaScript platform
• Built on Google's V8 high performance Javascript runtime. The same one Chrome uses.
• NOT JavaScript, but rather a set of fast backend tools written in C/C++.
• Designed with an asynchronous architecture that works pretty much like your browser.
• An event-driven, non-blocking I/O model perfect for data-intensive, real-time applications
• A way to run javascript outside of your browser.
• The only way (I know of) to build web sites end-to-end using only one language.
• Used in production by top products and companies
• A growing community and platform
6
Web servers are old.
• 20 years of Web servers had
one general design: 

Receive a connection.

Accept a client request.

Return a response.

Shut down the connection.
• By the early-mid 2000s, web
applications were significantly
more interactive, complex and
real-time.

• Completely avoiding the old
“stateless” request-response
paradigm was hard.
REQUEST
RESPONSE
7
This guy got fed up.
In 2006, a web developer named Ryan Dahl
got hired to build a snowboarding website.
And he “got into this feature where users
could upload images.”
FEATURE: “How do you give people
feedback on how much of the file has been
uploaded?”
Meant doing two things at the same time, but
the server side of things was messy.
Why was it so hard to check what was
happening in another connection?
So in 2009, he created Node.js.
NOTE: There were and still are a lot of solutions to deal with
real-time applications, I/O and user concurrency. (Scala/
Java, Python/Twisted, Ruby EventMachine, PHP React &
Go). But doing those things wasn’t particularly easy then
(and still isn’t).
8
99
What if everything was non-blocking?
How do we make concurrent requests and responses easier?
What if you never waited for any IO to happen?
10
How does it work?
11
Node works like your browser.
• Node.js is built on top of
Google’s V8 JavaScript
runtime.
• V8 takes code you write in
JavaScript, compiles it into
machine code, and
executes it.
• Similar to browser, Node
has a bunch of helpers that
can do other stuff in parallel.
12
Application
APIs
Low Level I/O Stuff
Javascript
Google JavaScript Engine


DOM
AJAX
Network
Filesystem
DISCLAIMER: GROSS
OVER-SIMPLIFICATION!
The power of the asynchronous callback.
• I/O can still be a huge bottleneck for
servers.
• Special sauce of Node is ability for code to
go off to execute somewhere else to do it’s
thing, while the rest of your code
continues.
• Code is called an “asynchronous callback.”
• HIGH LEVEL

Asynchronous callbacks are functions that
can go off on their own, do some stuff and
come back when they are done. (It’s those
parallel helpers that make it happen.)
• Event Loop running all the time picks up
executed code and sends it back to you.
13
How your code works in the browser.
call stackcode BrowserAPIs
task queue
DOM (document)
ajax (XMLHTTPRequest)
SetTimeout
checkEmailAddress() {
containsATsymbol();
endsWithTLDomain();
};
checkEmailAddress();
checkEmailAddress()
containsATsymbol()
endsWithTLDomain()
14
onClick() onMouseover() onFocus()
event loop
Architecture diagram and stack animation credited to Philip Roberts.
How your code executes on a Node.js server.
call stackcode NodeAPIs
task queue
File System
Network
Process
formatEmailAddress()
15
saveEmailToDB() sendWelcomeEmail() checkLocalUbers()
…and much more!
event loop
CB saveEmailToDB();
CB sendWelcomeEmail();
formatEmailAddress();
The fast food analogy.
1. Order is given to a cashier
taking one order at a time. 

(call stack!)
2. After ordering, you step aside.
3. Helpers in the back then work
on part of your order
(callbacks!)
4. And drop it in the pick up area
when it’s done. (task queue!)
5. Food is picked up by the
cashier as it is ready. 

(event loop!)
16
Loupe Demo
latentflip.com/loupe/
17
What are some great uses for Node?
18
Where Node.js shines.
When real-time, two-way interactivity with lots of concurrent users is a
requirement, consider Node.js.
• Chat
• Data Streaming
• Monitoring Dashboards
• Team-based GPS Applications
• Integrations (JSON)
• Internet of Things - health monitors, sensors, thermostats, cars and “smart”
devices that need to talk to disparate data sources and services both in real-
time and offline.
19
But Node.js isn’t perfect.
REASON #1

When you need something more powerful than JS.
REASON #2

When I/O is not your problem.
Applications that need heavy server-side computational
processing or simple content delivery won’t take advantage
of the platform.
20
Popular packages available via Node.js (NPM)
• Express
• Async.js
• Request
• Browserify
• Grunt
• PM2
• Socket.IO
• Commander.js
• Mocha
• Bower
21
• Underscore
• UglifyJS2
• Cheerio
• Passport
• Nodemailer
• Hapi
• React
• Karma
• Pug
• Node MySQL
• MongoDB
• JSHint
• Morgan
• LESS
• WebPack
• Angular
• Restify
• Marked
• GraphicsMagick
• JSDom
*Top 30 NPM Packages for Node.js Developers 2016 https://colorlib.com/wp/npm-packages-node-js/
Popular text editors and IDEs (for Node)
• Cloud 9

https://c9.io/
• Intellij IDEA

https://www.jetbrains.com/idea/
• WebStorm

https://www.jetbrains.com/webstorm/
• Komodo IDE

www.activestate.com/komodo-ide
• Koding

https://koding.com/
22
• Eclipse

https://eclipse.org/
• WebMatrix

https://www.microsoft.com/web/webmatrix/
• Sublime Text

https://www.sublimetext.com/
• Atom

https://atom.io/
• Visual Studio Code

https://code.visualstudio.com/
Who is using it?
23
Small and relatively new, the community continues to grow.
24
Enterprise corporations using Node.js
25
Case Studies
UBER https://eng.uber.com/tech-stack-part-two/

• Uber’s core trip execution engine originally written in Node.js

• Mobile client router API written in Node.js

• Systems that handle pings from riders and drivers in real-time and then match them written
in Node.js and Go.

• Core of our web tech stack is built on top of Node.js 

EBAY & PAYPAL
• Moved production from Java to Node.js

• 2x as fast with fewer people

• 33% fewer lines of code

• 40% fewer files

LINKEDIN
• 20x faster

• Frontend and backend mobile teams combined into single unit

• Servers cut from 30 to 3. Enough headroom to handle 10x current capacity.

NETFLIX

• Shifted UI to Node.js

GODADDY

• Started prototyping in 2013. 

• Revamped entire backend to Node.js. 

• Increased hosting capabilities

• Handled same load with 10% of the hardware.

ATOM —> VS CODE

• Written with Node.js!
26
Hello World!
2727
Resources
28
Learning More About Node.js
Online Training
• Node Hero - Getting Started With Node.js Tutorial

https://blog.risingstack.com/node-hero-tutorial-getting-started-with-node-js/

• You Don't Know Node.js

https://github.com/azat-co/you-dont-know-node

Books
• Node.js Design Patterns

Blog Posts
• Understanding Stateful Web Connections Using JavaScript

https://www.meteor.com/articles/stateful-web-connections-with-meteor

• Why The Hell Would I Use Node.js? A Case-by-Case Tutorial

https://www.toptal.com/nodejs/why-the-hell-would-i-use-node-js

• Architecture of Node.js’ Internal Codebase

https://arenli.com/architecture-of-node-js-internal-codebase-57cd8376b71f

Videos
• What the Heck is an Event Loop Anyway by Philip Roberts @ JSConf EU 2014

https://www.youtube.com/watch?v=8aGhZQkoFbQ

• Ryan Dahl Interview, Node Summit 2012

https://www.youtube.com/watch?v=Fc26auhSLqM

29
30

More Related Content

Introduction to Node.js

  • 2. –The Smiths “When you say it's gonna happen "now" Well when exactly do you mean? See I've already waited too long And all my hope is gone.” 2
  • 3. 3 “I hate almost all software…The only thing that matters in software is the experience of the user. ” -Ryan Dahl , Inventor of Node.js http://tinyclouds.org/rant.html
  • 4. 1. What is Node.js? 2. How does it work? 3. What can I do with it? 4. Who is using it? 5. “Hello World!” 6. Resources Aaron Rosenberg Mark Keith 4 Agenda
  • 6. What is Node.js®? • Server-side JavaScript platform • Built on Google's V8 high performance Javascript runtime. The same one Chrome uses. • NOT JavaScript, but rather a set of fast backend tools written in C/C++. • Designed with an asynchronous architecture that works pretty much like your browser. • An event-driven, non-blocking I/O model perfect for data-intensive, real-time applications • A way to run javascript outside of your browser. • The only way (I know of) to build web sites end-to-end using only one language. • Used in production by top products and companies • A growing community and platform 6
  • 7. Web servers are old. • 20 years of Web servers had one general design: Receive a connection.
 Accept a client request.
 Return a response.
 Shut down the connection. • By the early-mid 2000s, web applications were significantly more interactive, complex and real-time. • Completely avoiding the old “stateless” request-response paradigm was hard. REQUEST RESPONSE 7
  • 8. This guy got fed up. In 2006, a web developer named Ryan Dahl got hired to build a snowboarding website. And he “got into this feature where users could upload images.” FEATURE: “How do you give people feedback on how much of the file has been uploaded?” Meant doing two things at the same time, but the server side of things was messy. Why was it so hard to check what was happening in another connection? So in 2009, he created Node.js. NOTE: There were and still are a lot of solutions to deal with real-time applications, I/O and user concurrency. (Scala/ Java, Python/Twisted, Ruby EventMachine, PHP React & Go). But doing those things wasn’t particularly easy then (and still isn’t). 8
  • 9. 99
  • 10. What if everything was non-blocking? How do we make concurrent requests and responses easier? What if you never waited for any IO to happen? 10
  • 11. How does it work? 11
  • 12. Node works like your browser. • Node.js is built on top of Google’s V8 JavaScript runtime. • V8 takes code you write in JavaScript, compiles it into machine code, and executes it. • Similar to browser, Node has a bunch of helpers that can do other stuff in parallel. 12 Application APIs Low Level I/O Stuff Javascript Google JavaScript Engine 
 DOM AJAX Network Filesystem DISCLAIMER: GROSS OVER-SIMPLIFICATION!
  • 13. The power of the asynchronous callback. • I/O can still be a huge bottleneck for servers. • Special sauce of Node is ability for code to go off to execute somewhere else to do it’s thing, while the rest of your code continues. • Code is called an “asynchronous callback.” • HIGH LEVEL
 Asynchronous callbacks are functions that can go off on their own, do some stuff and come back when they are done. (It’s those parallel helpers that make it happen.) • Event Loop running all the time picks up executed code and sends it back to you. 13
  • 14. How your code works in the browser. call stackcode BrowserAPIs task queue DOM (document) ajax (XMLHTTPRequest) SetTimeout checkEmailAddress() { containsATsymbol(); endsWithTLDomain(); }; checkEmailAddress(); checkEmailAddress() containsATsymbol() endsWithTLDomain() 14 onClick() onMouseover() onFocus() event loop Architecture diagram and stack animation credited to Philip Roberts.
  • 15. How your code executes on a Node.js server. call stackcode NodeAPIs task queue File System Network Process formatEmailAddress() 15 saveEmailToDB() sendWelcomeEmail() checkLocalUbers() …and much more! event loop CB saveEmailToDB(); CB sendWelcomeEmail(); formatEmailAddress();
  • 16. The fast food analogy. 1. Order is given to a cashier taking one order at a time. 
 (call stack!) 2. After ordering, you step aside. 3. Helpers in the back then work on part of your order (callbacks!) 4. And drop it in the pick up area when it’s done. (task queue!) 5. Food is picked up by the cashier as it is ready. 
 (event loop!) 16
  • 18. What are some great uses for Node? 18
  • 19. Where Node.js shines. When real-time, two-way interactivity with lots of concurrent users is a requirement, consider Node.js. • Chat • Data Streaming • Monitoring Dashboards • Team-based GPS Applications • Integrations (JSON) • Internet of Things - health monitors, sensors, thermostats, cars and “smart” devices that need to talk to disparate data sources and services both in real- time and offline. 19
  • 20. But Node.js isn’t perfect. REASON #1
 When you need something more powerful than JS. REASON #2
 When I/O is not your problem. Applications that need heavy server-side computational processing or simple content delivery won’t take advantage of the platform. 20
  • 21. Popular packages available via Node.js (NPM) • Express • Async.js • Request • Browserify • Grunt • PM2 • Socket.IO • Commander.js • Mocha • Bower 21 • Underscore • UglifyJS2 • Cheerio • Passport • Nodemailer • Hapi • React • Karma • Pug • Node MySQL • MongoDB • JSHint • Morgan • LESS • WebPack • Angular • Restify • Marked • GraphicsMagick • JSDom *Top 30 NPM Packages for Node.js Developers 2016 https://colorlib.com/wp/npm-packages-node-js/
  • 22. Popular text editors and IDEs (for Node) • Cloud 9
 https://c9.io/ • Intellij IDEA
 https://www.jetbrains.com/idea/ • WebStorm
 https://www.jetbrains.com/webstorm/ • Komodo IDE
 www.activestate.com/komodo-ide • Koding
 https://koding.com/ 22 • Eclipse
 https://eclipse.org/ • WebMatrix
 https://www.microsoft.com/web/webmatrix/ • Sublime Text
 https://www.sublimetext.com/ • Atom
 https://atom.io/ • Visual Studio Code
 https://code.visualstudio.com/
  • 23. Who is using it? 23
  • 24. Small and relatively new, the community continues to grow. 24
  • 26. Case Studies UBER https://eng.uber.com/tech-stack-part-two/ • Uber’s core trip execution engine originally written in Node.js • Mobile client router API written in Node.js • Systems that handle pings from riders and drivers in real-time and then match them written in Node.js and Go. • Core of our web tech stack is built on top of Node.js EBAY & PAYPAL • Moved production from Java to Node.js • 2x as fast with fewer people • 33% fewer lines of code • 40% fewer files LINKEDIN • 20x faster • Frontend and backend mobile teams combined into single unit • Servers cut from 30 to 3. Enough headroom to handle 10x current capacity. NETFLIX • Shifted UI to Node.js GODADDY • Started prototyping in 2013. • Revamped entire backend to Node.js. • Increased hosting capabilities • Handled same load with 10% of the hardware.
 ATOM —> VS CODE • Written with Node.js! 26
  • 29. Learning More About Node.js Online Training • Node Hero - Getting Started With Node.js Tutorial
 https://blog.risingstack.com/node-hero-tutorial-getting-started-with-node-js/ • You Don't Know Node.js
 https://github.com/azat-co/you-dont-know-node Books • Node.js Design Patterns Blog Posts • Understanding Stateful Web Connections Using JavaScript
 https://www.meteor.com/articles/stateful-web-connections-with-meteor • Why The Hell Would I Use Node.js? A Case-by-Case Tutorial
 https://www.toptal.com/nodejs/why-the-hell-would-i-use-node-js • Architecture of Node.js’ Internal Codebase
 https://arenli.com/architecture-of-node-js-internal-codebase-57cd8376b71f Videos • What the Heck is an Event Loop Anyway by Philip Roberts @ JSConf EU 2014
 https://www.youtube.com/watch?v=8aGhZQkoFbQ • Ryan Dahl Interview, Node Summit 2012
 https://www.youtube.com/watch?v=Fc26auhSLqM
 29
  • 30. 30