Download - Basic Understanding and Implement of Node.js
![Page 1: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/1.jpg)
Basic Understanding and Implement of Node.js
Gary2014/04/18
![Page 2: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/2.jpg)
Outline
• Review of Node.js
• Event Loop• Basic Thesis
• Callback
• Asynchronous Mechanism
• Module
• Express
• Conclusion
![Page 3: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/3.jpg)
Review of Node.js
• A platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications.
• Uses an event-driven, non-blocking I/O model that makes it lightweight and efficient
• Perfect for data-intensive real-time applications that run across distributed devices
• Contains a built-in HTTP server library
• Making it possible to run a web server without the use of external software, such as Apache or Lighttpd
• Allowing more control of how the web server works
![Page 4: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/4.jpg)
Review of Node.js
• Feature of Node.js• V8 JavaScript Engine
• Single-threaded
• Event-driven
• Non-blocking I/O model
![Page 5: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/5.jpg)
Review of Node.js
![Page 6: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/6.jpg)
Event Loop – Basic Thesis
• The first basic thesis of node.js is that I/O is expensive
• So the largest waste with current programming technologies comes from waiting for I/O to complete.
![Page 7: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/7.jpg)
Event Loop – Basic Thesis
• There are several ways in which one can deal with the performance impact• Synchronous
• you handle one request at a time, each in turn.
• fork a new process: • you start a new process to handle each request.
• threads: • start a new thread to handle each request.
![Page 8: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/8.jpg)
Event Loop – Basic Thesis
• The second basis thesis is that thread-per-connection is memory-expensive
• Apache is multithreaded. It spawns a thread per request (or process, it depends on the conf).
• Node.js is not multithreaded, because threads and processes carry a heavy memory cost.
![Page 9: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/9.jpg)
Event Loop – Basic Thesis
Node.js App
Read file
Send file I/O request
Send HTTP request
Send Internet I/O request
SQL inquire
Send database I/O requestfile I/O response Internet I/O reponse
database I/O response
![Page 10: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/10.jpg)
Event Loop – Callback
Node.js App
Event Loop
Read file
Send file I/O request
Send HTTP request
Send Internet I/O request
SQL inquire
Send database I/O request
file I/O response
Internet I/O request
database I/O request
![Page 11: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/11.jpg)
Event Loop – Callback
• An event loop is “an entity that handles and processes external events and converts them into callback invocations”.
• At an I/O call, your code saves the callback and returns control to the node.js runtime environment.
• The callback will be called later when the data actually is available.
![Page 12: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/12.jpg)
Event Loop – Callback
• First-class functions• E.g. we pass around functions as data, shuffle them around and execute them
when needed.
• Function composition• Also known as having anonymous functions or closures that are executed
after something happens in the evented I/O.
function say(word) {
console.log(word);
}
function execute(someFunction, value) {
someFunction(value);
}
execute(say, "Hello");
function execute(someFunction, value) {
someFunction(value);
}
execute(function(word){ console.log(word) }, "Hello");
![Page 13: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/13.jpg)
Event Loop – Callback
• Run the node.js app, it will immediately output “Server has started”
• When sending request to server, “Request received” is shown
• This is an event-driven asynchronous server-side JavaScript and its callback
var http = require("http");
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
![Page 14: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/14.jpg)
Event Loop – Asynchronous Mechanism
• Node.js is event-driven design
• In web application, it is said to have the fastest real-time response
• The principle is to use libuv to implement event polling, constantly check for events that need to be processed
• If it finds an event on standby, run and trigger the corresponding handler.
![Page 15: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/15.jpg)
Event Loop – Asynchronous Mechanism
• Node.js keeps a single thread for your code
• Doing a “sleep” will block the server
• So while that code is running, node.js will not respond to any other requests from clients, since it only has one thread for executing your code.
• Or if you would have some CPU -intensive code, say, for resizing images, that would still block all other requests.
![Page 16: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/16.jpg)
Event Loop – Asynchronous Mechanism
• The console will shown “blah…” after the fileis read
file.open();
while(true) {
result = file.read();
if (!result)
break;
/* Do something... */
}
console.log('blah blah blah...');
Difficult task
Easy task
Difficult taskdone on time
Easy taskdelayed
![Page 17: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/17.jpg)
Event Loop – Asynchronous Mechanism
• process.nextTick(callback)• On the next loop around the event loop call this callback.
• This is not a simple alias to setTimeout(fn, 0), it's much more efficient. It typically runs before any other I/O events fires
• Node.js implements a queue storing events
• All event triggers are driven by the engine of a polling event.
• process.nextTick () is that we can put a program into the event queue, the next time the event polling is driven.
![Page 18: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/18.jpg)
Event Loop – Asynchronous Mechanism
Difficult task
Easy task
Easy task done.Strill delayed
difficult taskdone
function readLoop() {result = file.read();if (!result)
return;/* Do something... */process.nextTick(readLoop);
}file.open();process.nextTick(readLoop);console.log('blah blah blah...');
![Page 19: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/19.jpg)
Event Loop – Asynchronous Mechanism
• child_process.fork• Spawn Node processes
• Having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in.
• Assume at least 30ms startup and 10mb memory for each new Node. That is, you cannot create many thousands of them.
![Page 20: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/20.jpg)
Event Loop – Asynchronous Mechanism
var n = child_process.fork('./child.js');n.on('message', function(m) {
console.log('PARENT got message:', m);});n.send({ hello: 'world' });
process.on('message', function(m) {console.log('CHILD got message:', m);
});process.send({ foo: 'bar' });
Difficult task
Easy task 1
difficult taskdone
Easy task 2
Easy task 1 done
Easy task 2 done
![Page 21: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/21.jpg)
Event Loop – Asynchronous Mechanism
• Cluster• A single instance of Node runs in a single thread.
• To take advantage of multi-core systems the user will sometimes want to launch a cluster of Node processes to handle the load.
• The cluster module allows you to easily create child processes that all share server ports.
![Page 22: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/22.jpg)
Event Loop – Asynchronous Mechanismvar cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) { // Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' +
worker.process.pid + ' died');
});
} else { // Workers can share any TCP connection
// In this case its a HTTP server
http.createServer(function(req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
}
Difficult task
Easy task 1
Easy task 2
Easy task 1 done
v
Easy task 2 done
difficult taskdone
![Page 23: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/23.jpg)
Event Loop – Asynchronous Mechanism
![Page 24: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/24.jpg)
Module
• NPM(Node Package Manager)• The official package manager for Node.js.
• As of Node.js version 0.6.3, npm is bundled and installed automatically with the environment.
• Runs through the command line and manages dependencies for an application.
• Allows users to install Node.js applications that are available on the npmregistry.
• Written entirely in JavaScript, and runs on the Node.js platform.
![Page 25: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/25.jpg)
Module
• We use require and exports to connect with module
var http = require("http");
function start() {
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
Module can be download with npm
var server = require("./server");
server.start();
Module can be written by requirements
server.js index.js
![Page 26: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/26.jpg)
Express
• Express is a minimal and flexible node.js web application framework
• Providing a robust set of features for building single and multi-page, and hybrid web applications.
![Page 27: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/27.jpg)
Express
• >npm info express version
• edit package.json
• >npm install
{"name": "hello-world","description": "hello world test app","version": "0.0.1","private": true,"dependencies": {
"express": "4.0.0"}
}
![Page 28: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/28.jpg)
Express
• Node.js has more and more resources related to development,
• No need to write many features, you can directly use the module has been developed via official NPM tool from the others on the network
• Developers can focus wholeheartedly on the current development of applications.
• We always depend on a lot of third-party modules, and whenever we change the develop environment, it is necessary manually installing modules
• package.json file directly manage project dependencies and modules which being used by project
![Page 29: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/29.jpg)
Express
• Three outstanding features• routing distribution
• request processing
• rendering the view
var app = express();
app.get("/hello/:who", function(req, res) {
res.end("Hello, " + req.params.who + ".");
});
// 啓動Express
var express = require("express");
var app = express();
// 設置view目錄app.set("views", __dirname + "/views");
// 設置模板引擎app.set("view engine", "jade");
![Page 30: Basic Understanding and Implement of Node.js](https://reader033.vdocument.in/reader033/viewer/2022051415/55d18a5bbb61eb846f8b45db/html5/thumbnails/30.jpg)
Conclusion
• Web applications typically have bottleneck in I/O of network, and Node.js solves them well.
• Node.js uses JavaScript, which web developers already familiar with.
• NPM let everyone easily put theirs modules on.
• The disadvantage is that Node.js is new, not stable enough, API regularly updated.