Node intro

2,704 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,704
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
33
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Node intro

  1. 1. node.js evented I/O for V8 JavaScript
  2. 2. what is node? Server-side JavaScript done right. Runs on V8 An environment for developing high- performance web services Evented TCP stack Not a framework
  3. 3. why node? Web applications spend most of their time doing I/O JavaScript is the language of the web
  4. 4. V8
  5. 5. V8 Google's open source JavaScript engine. Developed by Lars Bak. Fast: compiles JavaScript to machine code. Implements most of ECMAScript 5.
  6. 6. ECMAScript 5
  7. 7. ECMAScript 5 Safe prototype extension Object.defineProperty(Object.prototype, "forEach", { value: function (callback) { var keys = Object.keys(this); for (var i = 0, key; i < keys.length; i++) { key = keys[i]; callback.call(this, key, this[key]); } } });
  8. 8. ECMAScript 5 Access to the hidden prototypes Object.getPrototypeOf([]) // Array [].__proto__ // Array [].__proto__.__proto__ // Object
  9. 9. ECMAScript 5 Basic prototypal inheritance var o = Object.create({ foo: 42 }); o.bar = "bah"; Object.keys(o) // ["bar"] o.foo // 42 o.__proto__ // { foo: 42 }
  10. 10. node.js
  11. 11. node.js Event-driven programming Asynchronous I/O Callbacks
  12. 12. node.js Common.js module system var sys = require("sys"); sys.puts("hello world");
  13. 13. node.js Common.js module system require.paths // ["./lib", ...] __dirname // this dirname __filename // this filename
  14. 14. node.js Simple HTTP server var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, { 'Content-Type':'text/plain' }); response.end('Hello Worldn'); }).listen(8000);
  15. 15. Event-driven programming
  16. 16. setTimeout(function () { // Do something after 1 second }, 1000);
  17. 17. process.nextTick(function () { // Do something asynchronously });
  18. 18. Async error handling process.addListener('uncaughtException', function (err) { // Handle exception });
  19. 19. Async signal handling process.addListener('SIGINT', function (err) { // Handle Ctrl-C });
  20. 20. modules
  21. 21. file-system module require('fs');
  22. 22. modules fs one-to-one mapping with unix commands most functions have a synchronous version
  23. 23. Asynchronous file stat fs.stat("path/to/file", function (err, res) { if (res) { // Handle success } else { // Handle error } });
  24. 24. Synchronous file stat var res = fs.statSync("path/to/file"); if (res) { // Handle success } else { // Handle error }
  25. 25. http://nodejs.org
  26. 26. @cloudhead Alexis Sellier http://github.com/cloudhead/node-intro

×