Your SlideShare is downloading. ×
0
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Introduction to node.js by Ran Mizrahi @ Reversim Summit

1,537

Published on

Node.js is a platform built on Chrome V8 javascript runtime engine for building fast and scalable, non-blocking, real-time and network applications. In this session I'll introduce you to node.js and …

Node.js is a platform built on Chrome V8 javascript runtime engine for building fast and scalable, non-blocking, real-time and network applications. In this session I'll introduce you to node.js and developing large code bases using it. We'll cover the following aspects:

* What is node.js?
* Apache vs. Nginx performance (One thread per connection vs. event loop) and what it has to do with node.js.
* Why node was written in Javascript?
* Main tools and frameworks (Express, socket.io, mongoose etc.)
* TDD/BDD with node.js using mocha and Expect.js

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,537
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Monday, April 1, 13
  • 2. Introduction to node.js Ran Mizrahi (@ranm8) Open Source Dpt. Leader @ CodeOasisMonday, April 1, 13
  • 3. About CodeOasis • CodeOasis specializes in cutting-edge web solutions. • Large variety of customers (from startups to enterprises). • Technologies we love: • PHP - Symfony2 and Drupal • node.js (-: • HTML5 • CSS3 • AngularJS • Our Microsoft department works with C#, WPF, etc.Monday, April 1, 13
  • 4. What is node.js?? • Server-side JavaScript development platform. • Built on top of Chrome’s JavaScript runtime engine V8. • Aims for easy development of scalable, non-blocking I/O, real time and network applications. • Written in C/C++ and JavaScript. • CommonJS module system. • node.js is single-threaded and uses event-loop.Monday, April 1, 13
  • 5. What is V8?? • V8 is Google Chromes JavaScript runtime engine. • Implements ECMAScript specification (5th edition). • Standalone and can be embedded to any C++ application. • Compiles JavaScript to native machine code before executing it instead of interpreting. • Open sourced under the new BSD license.Monday, April 1, 13
  • 6. We all love burgers!Monday, April 1, 13
  • 7. We all love burgers!Monday, April 1, 13
  • 8. Thread per Connection Burgers RestaurantMonday, April 1, 13
  • 9. Thread per Connection Burgers RestaurantMonday, April 1, 13
  • 10. Thread per Connection Burgers RestaurantMonday, April 1, 13
  • 11. Monday, April 1, 13
  • 12. Something is WRONG, we must do it BETTER!!!Monday, April 1, 13
  • 13. Event-driven Burgers RestaurantMonday, April 1, 13
  • 14. Event-driven Burgers RestaurantMonday, April 1, 13
  • 15. Apache vs. NGINX Performance Requests per second:Monday, April 1, 13
  • 16. Apache vs. NGINX Performance Memory usage:Monday, April 1, 13
  • 17. Apache vs. NGINX Performance So, what is the big difference between Apache and Nginx? • Apache uses one thread per connection. • Hard to scale. • Resource expensive. • NGINX is single-threaded and uses event-loop for handling requests. • Easy to scale. • Lower resources consumption.Monday, April 1, 13
  • 18. Blocking Code What is the software doing while it queries to the DB?!? var response = db.query(select * form users); // use the resultMonday, April 1, 13
  • 19. Blocking Code What is the software doing while it queries to the DB?!? var response = db.query(select * form users); // use the result In most cases, nothing (/:Monday, April 1, 13
  • 20. Blocking Code What is the software doing while it queries to the DB?!? var response = db.query(select * form users); // use the result In most cases, nothing (/: • Better software should handle I/O differently! It should multitask. • Other tasks should be performed while waiting..Monday, April 1, 13
  • 21. Non-blocking Code Non-blocking code: db.query(select * form users, function(result){ // Use the result });Monday, April 1, 13
  • 22. Non-blocking Code Non-blocking code: db.query(select * form users, function(result){ // Use the result }); This is how I/O should be handled in concurrency, when the DB will respond, the given function will be executed.Monday, April 1, 13
  • 23. So, Why Isn’t Everyone Using Non-blocking I/O This what we learn: puts(Enter you name:); var name = gets(); puts(Hello + name);Monday, April 1, 13
  • 24. So, Why Isn’t Everyone Using Non-blocking I/O This what we learn: puts(Enter you name:); var name = gets(); puts(Hello + name); Considered too complicated (:/ puts(Enter you name here:); gets(function(name) { puts(Hello + name); });Monday, April 1, 13
  • 25. Why JavaScript?! JavaScript is designed specifically to be used with an event-loop: • Anonymous functions, closures. • Only one callback at a time. • I/O through DOM event callbacks. • Web developers already know JavaScript (Makes the learning curve much smaller).Monday, April 1, 13
  • 26. The node.js project • Provides purely event-driven, non-blocking infrastructure to write high concurrency applications. • Uses JavaScript for easy development of asynchronies apps. • Open source and extendable module system. https://github.com/popular/starredMonday, April 1, 13
  • 27. Some ExamplesMonday, April 1, 13
  • 28. Hello Reversim Summit! setTimeout(function() { console.log(Reversim Summit!); }, 2000); console.log(Hello); • The program outputs “Hello”, then waits two seconds and outputs “Reversim Summit!”. • While waiting for the timeout to complete, node.js will keep adjusting other tasks. • Node exits automatically when nothing is left to do.Monday, April 1, 13
  • 29. Streaming HTTP Server var http = require(http); var server = http.createServer(function(request, response) { response.writeHead(200, { Content-Type: text/plain }); setTimeout(function() { response.end(Reversim Summit!n); }, 2000); response.write(Hellon); }); server.listen(8000);Monday, April 1, 13
  • 30. Streaming HTTP Server var http = require(http); var server = http.createServer(function(request, response) { response.writeHead(200, { Content-Type: text/plain }); setTimeout(function() { response.end(Reversim Summit!n); }, 2000); response.write(Hellon); }); server.listen(8000); Let’s benchmark that... and see the results..Monday, April 1, 13
  • 31. Streaming HTTP Server var http = require(http); var server = http.createServer(function(request, response) { response.writeHead(200, { Content-Type: text/plain }); setTimeout(function() { response.end(Reversim Summit!n); }, 2000); response.write(Hellon); }); server.listen(8000); Let’s benchmark that... and see the results.. 20 secs, with single thread is the result of non-blocking structure.Monday, April 1, 13
  • 32. DNS Resolver var dns = require(dns); console.log(resolving google.com...); dns.resolve(google.com, function(error, addresses) { if (error) throw error; console.log(found: + addresses.join(, )); }); Resolves “google.com” and outputs the result.Monday, April 1, 13
  • 33. Common Frameworks and ToolsMonday, April 1, 13
  • 34. NPM (Node.js package manager) npm is a package and dependency manager for node.js. Some of npm features: • Easy installation and publishing of node.js modules. • Manages module dependancies. • Easy to use. • Works in both global and local scope.Monday, April 1, 13
  • 35. NPM (Node.js package manager) npm usage: Installs package in the current local directory: $ npm install express Installs package globally $ npm install express -gMonday, April 1, 13
  • 36. Express Express is a minimal and flexible node.js web framework, providing robust set of features for building web applications. Taken from http://expressjs.com Some of express features: • Robust routing. • Redirection helpers. • View rendering and partials support. • Built on top of connect.Monday, April 1, 13
  • 37. Express Web Server example: var express = require(express); var app = express.createServer(); app.get(/, function(request, response) { // Return JSON encoded response response.json({ code: 200, message: OK, payload: null }); }); • Creates new express HTTP server with route for path “/”. • Returns JSON formatted response.Monday, April 1, 13
  • 38. Socket.IO Socket.IO aims to make real-time apps possible in every browser and mobile device, blurring the differences between transport mechanisms. Taken from http://socket.io Some of socket.io main features: • Supports multiple transport mechanisms (WebSocket, Flash and AJAX long-polling fallback, etc.). • Management of sockets rooms and namespaces. • Disconnections detection through heartbeats. • Reconnection support with buffering.Monday, April 1, 13
  • 39. Socket.IO - Simple notification example Server: var io = require(socket.io).listen(3000); io.on(connection, function(socket) { var notification = { body: Hello Reversim Summit! }; socket.emit(notification, notification, function(response) { console.log(response); }); }); Client: var socket = io.connect(http://localhost:3000); socket.on(notification, function(data, callback) { console.log(data.body); callback(Hello to you too!); });Monday, April 1, 13
  • 40. Mongoose Mongoose aims to provide elegant MongoDB object modeling (ODM) for node.js. Some of mongoose main features: • Allows creating DB schemas. • Management of sockets rooms and namespaces. • Disconnections detection through heartbeats. • Reconnection support with buffering.Monday, April 1, 13
  • 41. Mongoose Mongoose cat document example: var mongoose = require(mongoose); mongoose.connect(localhost, my_db); var CatSchema = mongoose.Schema({ name: { required: true, default: My cat } }); var Cat = mongoose.model(Cat, CatSchema); var kitty = new Cat({ name: Kati }); kitty.save(function(err) { if (err) throw err; console.log(Saved!) });Monday, April 1, 13
  • 42. TDD/BDD using Mocha and Expect.js Mocha Mocha is a feature-rich JavaScript test frameworks running on node and the browser, making asynchronies tests easy. Main features: • Supports both TDD and BDD styles. • Both browser and node support. • Proper exit status for CI support. • Really easy async tests. • node.js debugger support. • Highly flexible, choose and join the pieces yourself (spy library, assertion library, etc.).Monday, April 1, 13
  • 43. TDD/BDD using Mocha and Expect.js Expect.js Expect.js is a minimalistic assertion library based on should.js Main features: • BDD style. • Compatible with all test frameworks. • Both node.js and browser compatible. • Standalone assertion library.Monday, April 1, 13
  • 44. TDD/BDD using Mocha and Expect.js “Normal” test: var expect = require(expect.js); describe(Array, function() { describe(#indexOf(), function() { it(Expect -1 when the value is not present, function() { var array = [1, 2, 3]; expect(array.indexOf(4)).to.be(-1); }); }); }); Run it.. $ mocha --reporter spec Array #indexOf() ✓ Expect -1 when the value is not present 1 test complete (5 ms)Monday, April 1, 13
  • 45. TDD/BDD using Mocha and Expect.js “Async” test: var expect = require(expect.js); function asyncCall(val ,callback) { var prefix = - ; setTimeout(function() { var newString = val + prefix + OK; callback(newString); }, 500); } describe(asyncCall, function() { it(Add suffix that prefixed with - to the given string, function(done) { var testVal = Foo; asyncCall(testVal, function(response) { expect(response).to.contain(testVal + - OK); done(); }); }); }); Let’s run it...Monday, April 1, 13
  • 46. Use Case FXP • Real-time notifications, forum threads and posts. • 30,000 concurrency connections! • We started with version 0.4 )-: and moved to 0.6.. • Today, runs on one web server for serving all those concurrent connections..Monday, April 1, 13
  • 47. Thank you! Questions?Monday, April 1, 13

×