17
About Node Rick Chang

Node Web Development 2nd Edition: Chapter1 About Node

Embed Size (px)

DESCRIPTION

Node Web Development 2nd Edition: Chapter1 About Node

Citation preview

Page 1: Node Web Development 2nd Edition: Chapter1 About Node

About NodeRick Chang

Page 2: Node Web Development 2nd Edition: Chapter1 About Node

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

Page 3: Node Web Development 2nd Edition: Chapter1 About Node

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)

Page 4: Node Web Development 2nd Edition: Chapter1 About Node

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

Page 5: Node Web Development 2nd Edition: Chapter1 About Node

Server-side JavaScript

RingoJS (http://ringojs.org/)

Based on Mozilla Rhino and written in Java

Node

Based on Google V8

Page 6: Node Web Development 2nd Edition: Chapter1 About Node

JavasScript

loosely typed

dynamically extendable objects

anonymous closures

Page 7: Node Web Development 2nd Edition: Chapter1 About Node

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

Page 8: Node Web Development 2nd Edition: Chapter1 About Node

Extend Object Dynamically

people.js

The employee works hard Drive vehicle

Result

Page 9: Node Web Development 2nd Edition: Chapter1 About Node

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!!

Page 10: Node Web Development 2nd Edition: Chapter1 About Node

Node.js

CommonJS module system

Make variables local to a module

Demo code

Page 11: Node Web Development 2nd Edition: Chapter1 About Node

Demo

parent.js example.js

Page 12: Node Web Development 2nd Edition: Chapter1 About Node

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 });

Page 13: Node Web Development 2nd Edition: Chapter1 About Node

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

Page 14: Node Web Development 2nd Edition: Chapter1 About Node

Company

Yahoo!

LinkedIn

Replace Ruby on Rails to Node

Paypal

Page 15: Node Web Development 2nd Edition: Chapter1 About Node

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

Page 16: Node Web Development 2nd Edition: Chapter1 About Node

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

Page 17: Node Web Development 2nd Edition: Chapter1 About Node

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