Node Web Development 2nd Edition: Chapter1 About Node

Post on 06-May-2015

204 views 4 download

description

Node Web Development 2nd Edition: Chapter1 About Node

Transcript of Node Web Development 2nd Edition: Chapter1 About Node

About NodeRick Chang

What is Node

A Platform to write JavasScript applications outside web browsers.

No DOM

No bowser capability

Act as a web server

Care about HTTP headers

About Node

No thread (run single-threaded)

Event-driven architecture (publish/subscribe)

Google V8 JavaScript engine

Non-block I/O event loop (Asynchronous I/O)

File and network I/O libraries (use multiple threads)

History

Ryan Dahl created in 2009[1]

chose JavaScript because of the lack of an existing I/O API

Build high performance and scalable network applications

Sponsored by Joyent[2]

Inspired By seeing a file upload progress bar on Flickr

npm created by Isaac Schlueter, a package manager was introduced in 2011

Microsoft partnered to create a native Windows version in July 2011

Server-side JavaScript

RingoJS (http://ringojs.org/)

Based on Mozilla Rhino and written in Java

Node

Based on Google V8

JavasScript

loosely typed

dynamically extendable objects

anonymous closures

Pub/Sub Pattern

pub.sub.js Resultfoo 1 2 foo.bar 1 2 foo.baz 1 2 !foo.bar 3 4 !foo.baz 5 6 !foo 7 8 foo.baz 7 8

Extend Object Dynamically

people.js

The employee works hard Drive vehicle

Result

JavaScript vs Java

Java

Non-dynamic language

Static typed

The compiler catch programming mistakes

JavaScript

Dynamic language

Loosely typed

Global Object

Global Object cause an unruly chaos!!

Node.js

CommonJS module system

Make variables local to a module

Demo code

Demo

parent.js example.js

Asynchronous I/O

A single-thread event loop without thread context switch

Callback function to handle result

result = query('SELECT * from db'); // operate on the result

query('SELECT * from db', function (err, result) { if (err) throw err; // handle errors // operate on result });

Benchmark

var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/');

Node Nginx

Throughput 822 req/sec 704 req/sec

Memory Used 64M 4M

Company

Yahoo!

LinkedIn

Replace Ruby on Rails to Node

Paypal

Conclusion

Node is a great platform for I/O-bound applications

No good for computationally expensive calculations

Finbonacci sequence calcuation

More performance, fewer servers, lower cost and lower environment impact

Debate

Node.js favours performance over usability and robustness

Difficult to debug, refactor and develop

no synchronous code

duplicate callback

emitters may get multiple “error” events

missing “error” events sends everything to hell

“error” handlers are very verbose

Reference

[1]Node.js, http://en.wikipedia.org/wiki/Node.js

[2]Joyent, https://www.joyent.com/

[3]Farewell Node.js, TJ Holowaychuk, https://medium.com/code-adventures/farewell-node-js-4ba9e7f3e52b

[4]关于TJ⼤大神的Farewell Node.js, http://www.welefen.com/about-tj-farewell-node.js-article.html

[5]JavaScript模块化开发(⼆二)—CommonJS规范, http://www.feeldesignstudio.com/2013/09/javascript-module-pattern-commonjs

[6]CommonJS规范, http://javascript.ruanyifeng.com/nodejs/commonjs.html