SlideShare a Scribd company logo
Introdution to Node.js
What’s Node?
What’s Node?

Evented I/O for V8 JavaScript
Evented I/O


• Don’t wait for I/O to complete
• Instead, do useful stuff on the CPU
• When I/O has finished, execute callback code
An Example

File.open('/etc/motd', 'w') do |f|
 logger.debug(f.read)
end
An Example

fs.readFile '/etc/motd', (err, data) ->
  console.log(data) unless err
An Example
fs.readFile('/etc/motd', function(err, data) {
  if (!err) {
    return console.log(data);
  }
});
Evented I/O


• Prior to Node, no I/O libraries for JavaScript
Modules


• Organise functionality
• No interference with other code
Loading a module

  hello = require('hello')
  hello.world()
hello.coffee

exports.world = () ->
 console.log('Hello World')


hello = require('hello')
hello.world()
Listening to Events

hello_world = new HelloWorld()

hello_world.on 'generated', (text) ->
  console.log text
Emitting Events
class HelloWorld extends EventEmitter
 generate: ->
   this.emit 'generated', 'Hello World!'

hello_world.on 'generated', (text) ->
  console.log text
Gems?


• The Node Package Manage (NPM) is awesome
• Find packages at http://search.npmjs.org/
• Node, and packages, are asynchronous by design
Consider this
http = require('http');
http.createServer (req, res) ->
  respond_later = () ->
    res.writeHead 200, {'Content-Type': 'text/plain'}
    res.end 'Thank you for waiting 2 secondsn'
  setTimeout respond_later, 2000
.listen(1337, "127.0.0.1")
Got Speed?

1000 concurrent users
  10 requests / user
    1 node server

What’s your estimate?
Watch it online...
   http://vimeo.com/ariejan/jmeter-
          benchmark-of-nodejs
Got Speed?

    1000 concurrent users
      10 requests / user
        1 node server

Average Response Time: 2007 ms
It’s got speed...
    ... but does it offer quality?
Take your vows


• Asynchronous Behaviour Driven Development
An example, again...

   JSONToolbox.com
Honour your vows
  API Service

 Random password with a negative length of -32
   ✓ should receive a password of length 32
 Random password with a length > 256
   ✓ should receive a password of length 256
 Random password with length 40
   ✓ should receive a password of length 40
 Random password with default length
   ✓ should receive a valid response
   ✓ should receive a password of length 12 by default
 User agent
   ✓ should report the user agent string
 IP Address
   ✓ should report client IP address as 127.0.0.1
 Ping
   ✓ should respond with pong

✓ OK » 10 honored (0.087s)
Honour your vows


✓ OK » 10 honored (0.087s)
Got Ping?

app.get '/api/ping', (req, res) ->
 res.contentType('application/json')
 res.send { "pong" : true }
Enter Zombies


• Headless, full-stack testing
• It’s capybara, but faster
• Oh, of course it’s asynchronous as well
Got Pong?

vows
 .describe('API Service')
 .addBatch
   'Ping':
     topic: -> zombie.visit("http://127.0.0.1/api/ping", this.callback)

    'should respond with pong': (browser) ->
      JSON.parse(browser.text('body'))['pong'].should.be.true
Useful stuff
• coffee-script - duh!
• express - Sinatra inspired web framework
• socket.io - cross-browser websockets
• vows, zombie - BDD
• nodeunit - TDD / Unit testing
• redis - Fast in memory DB, with PubSub support
Link Fest
• http://nodejs.org
• http://vowsjs.org
• http://zombie.labnotes.org
• http://search.npmjs.org
• https://github.com/ariejan/jsontoolbox
• https://github.com/ariejan/apocalypse
Q&A
Thank you!
      Good Night!

More Related Content

PPTX
Ansible module development 101
PDF
Introducing Ansible
PDF
Getting Started with Ansible
PDF
Infrastructure = Code
PDF
Jenkins and ansible reference
PDF
docker build with Ansible
PPT
Ansible presentation
PDF
Ansible is the simplest way to automate. SymfonyCafe, 2015
Ansible module development 101
Introducing Ansible
Getting Started with Ansible
Infrastructure = Code
Jenkins and ansible reference
docker build with Ansible
Ansible presentation
Ansible is the simplest way to automate. SymfonyCafe, 2015

What's hot (20)

PDF
Ansible Crash Course
PDF
A quick intro to Ansible
PPTX
Go Faster with Ansible (AWS meetup)
PPTX
Ansible presentation
PDF
Ansible and AWS
PPTX
Monitor-Driven Development Using Ansible
PDF
Network Automation with Ansible
PDF
Testing Ansible with Jenkins and Docker
PDF
Introduction to Ansible (Pycon7 2016)
PDF
Celery for internal API in SOA infrastructure
PPTX
Go Faster with Ansible (PHP meetup)
PDF
Ansible
PDF
IT Automation with Ansible
PDF
Chef Provisioning a Chef Server Cluster - ChefConf 2015
PPTX
Monitoring and tuning your chef server - chef conf talk
PPTX
Ansible Automation Best Practices From Startups to Enterprises - Minnebar 12
PDF
Ansible Oxford - Cows & Containers
PPTX
Ansible Tutorial For Beginners | What Is Ansible And How It Works? | Ansible ...
PPTX
Controlling multiple VMs with the power of Python
Ansible Crash Course
A quick intro to Ansible
Go Faster with Ansible (AWS meetup)
Ansible presentation
Ansible and AWS
Monitor-Driven Development Using Ansible
Network Automation with Ansible
Testing Ansible with Jenkins and Docker
Introduction to Ansible (Pycon7 2016)
Celery for internal API in SOA infrastructure
Go Faster with Ansible (PHP meetup)
Ansible
IT Automation with Ansible
Chef Provisioning a Chef Server Cluster - ChefConf 2015
Monitoring and tuning your chef server - chef conf talk
Ansible Automation Best Practices From Startups to Enterprises - Minnebar 12
Ansible Oxford - Cows & Containers
Ansible Tutorial For Beginners | What Is Ansible And How It Works? | Ansible ...
Controlling multiple VMs with the power of Python
Ad

Similar to Introdution to Node.js (20)

PDF
Introduction to Node.js
KEY
node.js: Javascript's in your backend
PDF
Node.js introduction
KEY
A language for the Internet: Why JavaScript and Node.js is right for Internet...
PPTX
introduction to node.js
KEY
Introduction to NodeJS with LOLCats
PPTX
System insight without Interference
KEY
Writing robust Node.js applications
PPTX
Node.js: The What, The How and The When
PDF
Introduction to Node.js: What, why and how?
PDF
CouchDB for Web Applications - Erlang Factory London 2009
PDF
Basic Understanding and Implement of Node.js
PDF
How do event loops work in Python?
PPTX
Intro to node and mongodb 1
KEY
A language for the Internet: Why JavaScript and Node.js is right for Internet...
KEY
Node.js - The New, New Hotness
PPTX
Make BDD great again
PDF
Python, do you even async?
PDF
Performance patterns
PPTX
GeekCampSG - Nodejs , Websockets and Realtime Web
Introduction to Node.js
node.js: Javascript's in your backend
Node.js introduction
A language for the Internet: Why JavaScript and Node.js is right for Internet...
introduction to node.js
Introduction to NodeJS with LOLCats
System insight without Interference
Writing robust Node.js applications
Node.js: The What, The How and The When
Introduction to Node.js: What, why and how?
CouchDB for Web Applications - Erlang Factory London 2009
Basic Understanding and Implement of Node.js
How do event loops work in Python?
Intro to node and mongodb 1
A language for the Internet: Why JavaScript and Node.js is right for Internet...
Node.js - The New, New Hotness
Make BDD great again
Python, do you even async?
Performance patterns
GeekCampSG - Nodejs , Websockets and Realtime Web
Ad

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Encapsulation theory and applications.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Cloud computing and distributed systems.
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPT
Teaching material agriculture food technology
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Machine learning based COVID-19 study performance prediction
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Encapsulation_ Review paper, used for researhc scholars
The Rise and Fall of 3GPP – Time for a Sabbatical?
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Encapsulation theory and applications.pdf
Electronic commerce courselecture one. Pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Cloud computing and distributed systems.
Building Integrated photovoltaic BIPV_UPV.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Big Data Technologies - Introduction.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Teaching material agriculture food technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Unlocking AI with Model Context Protocol (MCP)
Machine learning based COVID-19 study performance prediction
sap open course for s4hana steps from ECC to s4
Reach Out and Touch Someone: Haptics and Empathic Computing
“AI and Expert System Decision Support & Business Intelligence Systems”

Introdution to Node.js

Editor's Notes

  • #2: Welcome et. all.\n
  • #3: This talk is about Node.js, or Node. Does anyone know what Node is, actually?\n
  • #4: Node brings Evented I/O to the V8 JavaScript Engine. V8 is Google’s open source JavaScript engine, it’s used in Google Chrome.\n
  • #5: Evented I/O makes Node lightning fast. While waiting for some I/O to happen, it continues to utilize the CPU. Let’s skip to an example, shall we?\n
  • #6: Reading a file from disk, slow I/O. See this ruby example. If f.read takes X seconds, execution is blocked for X seconds - nothing else happens.\n
  • #7: Node goes do other stuff while waiting for I/O to complete. When the data is ready (or an error occurred), execute the callback function to continue processing the data. This is Coffee script btw.\n
  • #8: Okay, plain JavaScript for the non-coffeescripters.\n
  • #9: \n
  • #10: Modules allow you to organise functionality in logic units. These units can’t interfere with each other, because they are .. modular.\n\nTell about how JS files loaded all use the same variable scope.\n
  • #11: This is how you load a module in Coffee Script. Then you can call a method from that module.\n
  • #12: The hello module implementation.\n
  • #13: Events are an awesome pattern. Here’s an example.\n
  • #14: This is how you’d write the implementation. Of course in coffee script. Now wait for someone to call hello_world.generate()\n
  • #15: NPM\nNode was designed from the ground up. There is no ‘sync-node’, everything was designed to be async. Including packages for everything.\n
  • #16: A simple Node HTTP Server. When a request comes in we set a 2 second timer and then respond. Each request should take about 2 seconds to complete. \n
  • #17: Take a 1000 **concurrent** users; who’ll send 10 requests in succession each. How long does it take to run this test? For time’s sake, I’ll show you a video instead of running it live.\n\n10k requests at 2 seconds each on 1 server: 20k seconds, about 5.5 hours\n
  • #18: Show em! - http://vimeo.com/ariejan/jmeter-benchmark-of-nodejs\n
  • #19: Be amazed. Requests return on average after 2007ms. This means Node is not waiting 2 seconds before accepting the next request. When a timer goes, the response is send immediately.\n
  • #20: How about testing?\n
  • #21: Async tests! So they aren’t blocking on I/O either and therefore are lightning fast, just like Node is!\n
  • #22: JSONToolbox. A Node project by yours truly.\n
  • #23: Are my vows honoured? Yes the are ;-)\n
  • #24: DO YOU SEE IT?! Yes, 10 specs, including starting and shutting down a server, in 0.087s!\n
  • #25: Okay, first, here’s what we got. But, this is an API. Where's my capybara?\n
  • #26: We’ll need full-stack testing here to make sure everything works ‘in the browser’. We’ll use Zombie.js\n
  • #27: We’ll describe the API Service. Batches are executed sequentially. Tests within a batch are run asychroniously.\n
  • #28: Some packages I’ve found to be useful.\n
  • #29: Some links\n
  • #30: Any questions?\n
  • #31: Bye bye\n