26
node.js evented I/O for V8 JavaScript

Node intro

Embed Size (px)

Citation preview

Page 1: Node intro

1/31 - 4%

node.jsevented I/O for V8 JavaScript

Page 2: Node intro

2/31 - 7%

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

Page 3: Node intro

3/31 - 10%

why node?Web applications spend most of their timedoing I/O

JavaScript is the language of the web

Page 4: Node intro

4/31 - 13%

V8

Page 5: Node intro

5/31 - 17%

V8Google's open source JavaScript engine.

Developed by Lars Bak.

Fast: compiles JavaScript to machinecode.

Implements most of ECMAScript 5.

Page 6: Node intro

7/31 - 23%

ECMAScript 5

Page 7: Node intro

8/31 - 26%

ECMAScript 5

Safe prototype extensionObject.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]); } }});

Page 8: Node intro

9/31 - 30%

ECMAScript 5

Access to the hidden prototypesObject.getPrototypeOf([]) // Array[].__proto__ // Array [].__proto__.__proto__ // Object

Page 9: Node intro

10/31 - 33%

ECMAScript 5

Basic prototypal inheritancevar o = Object.create({ foo: 42 });

o.bar = "bah";

Object.keys(o) // ["bar"]

o.foo // 42o.__proto__ // { foo: 42 }

Page 10: Node intro

11/31 - 36%

node.js

Page 11: Node intro

12/31 - 39%

node.js

Event-driven programming

Asynchronous I/O

Callbacks

Page 12: Node intro

13/31 - 42%

node.js

Common.js module systemvar sys = require("sys");

sys.puts("hello world");

Page 13: Node intro

14/31 - 46%

node.js

Common.js module systemrequire.paths // ["./lib", ...]__dirname // this dirname__filename // this filename

Page 14: Node intro

15/31 - 49%

node.js

Simple HTTP servervar http = require('http');

http.createServer(function (request, response) { response.writeHead(200, { 'Content-Type':'text/plain' }); response.end('Hello World\n');}).listen(8000);

Page 15: Node intro

16/31 - 52%

Event-drivenprogramming

Page 16: Node intro

17/31 - 55%

setTimeout(function () { // Do something after 1 second}, 1000);

Page 17: Node intro

18/31 - 59%

process.nextTick(function () { // Do something asynchronously});

Page 18: Node intro

19/31 - 62%

Async error handlingprocess.addListener('uncaughtException', function (err) { // Handle exception });

Page 19: Node intro

20/31 - 65%

Async signal handlingprocess.addListener('SIGINT', function (err) { // Handle Ctrl-C });

Page 20: Node intro

21/31 - 68%

modules

Page 21: Node intro

22/31 - 71%

file-system modulerequire('fs');

Page 22: Node intro

23/31 - 75%

modules

fsone-to-one mapping with unix commands

most functions have a synchronousversion

Page 23: Node intro

24/31 - 78%

Asynchronous file statfs.stat("path/to/file", function (err, res) { if (res) { // Handle success } else { // Handle error }});

Page 24: Node intro

25/31 - 81%

Synchronous file statvar res = fs.statSync("path/to/file");

if (res) { // Handle success } else { // Handle error }

Page 25: Node intro

29/31 - 94%

http://nodejs.org

Page 26: Node intro

30/31 - 97%

@cloudheadAlexis Sellier

http://github.com/cloudhead/node-intro