Tải bản đầy đủ (.pdf) (107 trang)

Giới thiệu ngắn về Nodejs

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (11.37 MB, 107 trang )


INTRO TO NODE.JS
- LEVEL ONE -
INTRO TO NODE.JS
WHAT IS NODE.JS?
It’s fast because it’s mostly C code
Allows you to build scalable network
applications using JavaScript on the server-side.
V8 JavaScript Runtime
Node.js
INTRO TO NODE.JS
WHAT COULD YOU BUILD?

Websocket Server

Fast File Upload Client

Ad Server

Any Real-Time Data Apps
Like a chat server
INTRO TO NODE.JS
WHAT IS NODE.JS NOT?

A Web Framework

For Beginners
It’s very low level

Multi-threaded
You can think of it as a single threaded server




INTRO TO NODE.JS
OBJECTIVE: PRINT FILE CONTENTS
This is a “Callback”
Read file from Filesystem, set equal to “contents”
Print contents

Blocking Code

Non-Blocking Code
Do something else
Read file from Filesystem
whenever you’re complete, print the contents
Do Something else

console.log(contents);
INTRO TO NODE.JS
BLOCKING VS NON-BLOCKING
var contents = fs.readFileSync('/etc/hosts');
console.log(contents);
console.log('Doing something else');

Blocking Code

Non-Blocking Code
console.log('Doing something else');
Stop process until complete
fs.readFile('/etc/hosts', function(err, contents) {
});

fs.readFile('/etc/hosts', function(err, contents) {
console.log(contents);
});
INTRO TO NODE.JS
CALLBACK ALTERNATE SYNTAX
var callback = function(err, contents) {
console.log(contents);
}
fs.readFile('/etc/hosts', callback);
Same as
INTRO TO NODE.JS
BLOCKING VS NON-BLOCKING
blocking
0s
non-blocking
10s5s
0s 10s5s
fs.readFile('/etc/hosts', callback);
fs.readFile('/etc/inetcfg', callback);
var callback = function(err, contents) {
console.log(contents);
}
hello.js


NODE.JS HELLO DOG
$ curl http://localhost:8080
Hello, this is dog.
How we require modules
Status code in header

Response body
Close the connection
Listen for connections on this port

$ node hello.js
Run the server
var http = require('http');
http.createServer(function(request, response) {
response.writeHead(200);
response.write("Hello, this is dog.");
response.end();
}).listen(8080);
console.log('Listening on port 8080...');
Listening on port 8080...
THE EVENT LOOP
var http = require('http');
http.createServer(function(request, response) {
}).listen(8080);
console.log('Listening on port 8080...');
Starts the Event Loop when finished
...
Known Events
request
Checking
for
Events
Run the Callback
INTRO TO NODE.JS
WHY JAVASCRIPT?
“JavaScript has certain characteristics that make it very

different than other dynamic languages, namely that it has
no concept of threads. Its model of concurrency is
completely based around events.”
- Ryan Dahl
THE EVENT LOOP
Known Events
request
Checking
for
Events
connection
close
Event Queue
close
request
Events processed one at a time

INTRO TO NODE.JS
WITH LONG RUNNING PROCESS
Represent long running process
var http = require('http');
http.createServer(function(request, response) {
}).listen(8080);
response.writeHead(200);
response.write("Dog is done.");
response.end();
setTimeout(function(){
}, 5000);
5000ms = 5 seconds
response.write("Dog is running.");


INTRO TO NODE.JS
TWO CALLBACKS HERE
var http = require('http');
http.createServer(function(request, response) {
response.writeHead(200);
request
timeout
}).listen(8080);
response.write("Dog is done.");
response.end();
setTimeout(function(){
}, 5000);
response.write("Dog is running.");
TWO CALLBACKS TIMELINE
0s 10s5s
Request comes in, triggers request event
Request Callback executes
setTimeout registered
Request comes in, triggers request event
Request Callback executes
setTimeout registered
triggers setTimeout event
setTimeout Callback executes
triggers setTimeout event
setTimeout Callback
request
timeout
WITH BLOCKING TIMELINE
0s 10s5s

Request comes in, triggers request event
Request Callback executes
setTimeout executed
Request comes in, waits for server
Request comes in
triggers setTimeout event
setTimeout Callback executed
Wasted Time
Request Callback executes
INTRO TO NODE.JS

Calls out to web services
TYPICAL BLOCKING THINGS

Reads/Writes on the Database

Calls to extensions
EVENTS
- LEVEL TWO -
EVENTS
EVENTS IN THE DOM
The DOM
The DOM triggers Events
click
events
you can listen for those events
submit
hover
When ‘click’ event is triggered
attach

$("p").on("click", function(){ ... });
EVENTS
EVENTS IN NODE
EventEmitter
Many objects in Node emit events
net.Server
request
event
EventEmitter
fs.readStream
data
event
EVENTS
CUSTOM EVENT EMITTERS



var logger = new EventEmitter();
logger.emit('error', 'Spilled Milk');
ERR: Spilled Milk

logger.emit('error', 'Eggs Cracked');

var EventEmitter = require('events').EventEmitter;
error warn info
listen for error event
logger.on('error', function(message){
console.log('ERR: ' + message);
});
ERR: Eggs Cracked

events
EVENTS
EVENTS IN NODE
EventEmitter
Many objects in Node emit events
net.Server
request
event
When ‘request’ event is emitted
function(request, response){ .. }
emit
attach
EVENTS
HTTP ECHO SERVER
http.createServer(function(request, response){ ... });
But what is really going on here?
/>EVENTS
BREAKING IT DOWN
http.createServer(function(request, response){ ... });

×