Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

introduction to node.js

An introduction slides to node.js development given at nodejs IL meetup, 15/01/2013 TLV.
demo gist -

  • Be the first to comment

introduction to node.js

  1. 1. Introduction to node.jsEnd to end web development w/ javascript Or Kaplan
  2. 2. What is node? Taking JS Beyond the BrowserNode.js is a framework for building scalable server-side applications and network oriented programs with asynchronous javascript.
  3. 3. External modulesNode modules (fs,tcp http)Node internals Google V8
  4. 4. Full JavaScript Stack•
  5. 5. Before we start• JavaScript JavaScript JavaScript
  6. 6. Hello world examplesetTimeout(function () { console.log("world");}, 2000);console.log("hello");setInterval(function () { console.log("world");}, 2000);console.log("hello");
  7. 7. The non-blocking notion• Single threaded• Instead of waiting use events• Never wait for IO (socket, disk etc.)• JS is natural for building async programs• For blocking operation the node internals uses thread pool to wait for operations to finish.
  8. 8. Traditional I/Ovar data =;process(data);
  9. 9. Traditional I/Ovar data =;ZzZzZZZzz…process(data); Why wasting those cycles?!
  10. 10. Non-Blocking I/, function (data) { process(data); return success;});DoWhateverYouWishMeanwhile();
  11. 11. Node ModulesThe true force of node
  12. 12. NPM• NPM is a package manager for node.js –
  13. 13. Express Package• RESTful module for node webapps• Supports cookies, sessions, caching etc.•
  14. 14. Example of Express APIvar Express = require(express), app = Express.createServer();app.get(/users/(:user)/?, function (req, res) { res.send(hello + req.params.user);});app.listen(process.env.PORT || process.argv[3] || 8080);
  15. 15. Comet Use Case• Comet is a way for the client to get a real time event from the server• How would you implement? – Polling – using AJAX to poll for events for events – Long Polling – Send HTTP requests in chain, the response is delayed. – Streaming – Keep open socket to the server.
  16. 16. Requirements• Comet servers need to maintain many open connections• Holding one thread per connection is unacceptable• Node.js approach is better – easier to scale, less resources per connection.
  17. 17. Implementing using web sockets• Introduced on HTML5• Creating a thin layer over TCP/IP accepting constrain of the web• Supported by node.js• On this Demo we will use the module, which is HTML4 compatible web socket
  18. 18. Chat Demo Chat server in less than 40 lines of code
  19. 19. Buffering vs. Streaming
  20. 20. Exec – buffering (callback)var util = require(util), exec = require(child_process).exec, child;child = exec(cat *.js bad_file | wc -l, function (error, stdout, stderr) { console.log(stdout: + stdout); console.log(stderr: + stderr); if (error !== null) { console.log(exec error: + error); } });
  21. 21. Spawn - streamingvar util = require(util), spawn = require(child_process).spawn, ls = spawn(ls, [-lh, /usr]);ls.stdout.on(data, function (data) { console.log(stdout: + data);});ls.stderr.on(data, function (data) { console.log(stderr: + data);});ls.on(exit, function (code) { console.log(child process exited with code + code);});
  22. 22. Live Site Tips• Test your code• Run static analysis• Monitor your app• Let it scale• Continuous deployment• Use process manager- SMF, forever or upstart• Document your code• Share code between client and server• Explore the community• Global uncaught exception handler• Chaos monkey
  23. 23. Why should one use node.js• You already code your client using JS• Great Performance-• Can support thousands of concurrent connections• Easy to scale• Ideal for the mobile era• Fast development• Easy debug• No locks - V8 uses only one thread!• Community
  24. 24. Good For• Small-Medium projects• Prototyping• Fast scale servers• Flexible dynamic application (inject code)• Web sockets
  25. 25. Limitations• New programming style• Using only one thread• Immature environment• Limited stack trace• A bug may crash the whole server – use forever watch dog.• Dynamic language – what’s your religion?
  26. 26. Some Patterns• Always return values on last statement• Pass callbacks• Code is not linear avoid infinite indentation (extract callback into named functions)• Promises• Test using node unit – high coverage is crucial
  27. 27. Debugging (V8)• Builtin debugger-• Ndb -• Inspector – web based (webkit) debugger -• Web storm -• Eclipse - Eclipse-as-Node-Applications-Debugger
  28. 28. CoffeeScript• A ruby like scripting language that compiles to JavaScript.• Quick guide -• Great slides at
  29. 29. References• Node.js –• Comet -• Comet -• WebSockets -• Best Practices - coding/• Best Practices -• Node modules -• Node on Production -
  30. 30. Questions? Or Kaplan