SlideShare a Scribd company logo
1 of 30
Download to read offline
CS142 Lecture Notes - Node.js
Node.js
Mendel Rosenblum
CS142 Lecture Notes - Node.js
Threads versus Events
request = readRequest(socket);
reply = processRequest(request);
sendReply(socket, reply);
Implementation:
Thread switching (i.e. blocking) and a
scheduler
startRequest(socket);
listen("requestAvail", processRequest);
listen("processDone", sendReplyToSock);
Implementation:
Event queue processing
CS142 Lecture Notes - Node.js
Threads versus Events using Callbacks
request = readRequest(socket);
reply = processRequest(request);
sendReply(socket, reply);
Implementation:
Thread switching (i.e. blocking) and a
scheduler
readRequest(socket, function(request) {
processRequest(request,
function (reply) {
sendReply(socket, reply);
});
});
Implementation:
Event queue processing
CS142 Lecture Notes - Node.js
Event queue
● Inner loop
while (true) {
if (!eventQueue.notEmpty()) {
eventQueue.pop().call();
}
}
● Never wait/block in event handler . Example readRequest(socket);
1. launchReadRequest(socket); // Returns immediately
2. When read finishes: eventQueue.push(readDoneEventHandler);
CS142 Lecture Notes - Node.js
Node.js
● Take a JavaScript engine from a browser (Chrome's V8 JavaScript Engine)
○ Get same JavaScript on both browser and server
○ Don't need the DOM on the server
● Include DOM-like events and an event queue
○ Everything runs as a call from the event loop (already had one for browser events)
● Make event interface to all OS operations
○ Wrap all the OS blocking calls (file and socket/network io)
○ Add some data handle support
● Add a proper module system (predated import/export)
○ Each module gets its own scope (not everything in window)
CS142 Lecture Notes - Node.js
Example: Node.js reading a file
let fs = require("fs"); // require is a Node module call
// fs object wraps OS sync file system calls
// OS read() is synchronous but Node's fs.readFile is asynchronous
fs.readFile("smallFile", readDoneCallback); // Start read
function readDoneCallback(error, dataBuffer) {
// Node callback convention: First argument is JavaScript Error object
// dataBuffer is a special Node Buffer object
if (!error) {
console.log("smallFile contents", dataBuffer.toString());
}
}
CS142 Lecture Notes - Node.js
Node Modules
● Import using require() - Can use ES6 import if file name *.mjs
○ System module: require("fs"); // Looks in node_modules directories
○ From a file: require("./XXX.js"); // Reads specified file
○ From a directory: require("./myModule"); // Reads myModule/index.js
● Module files have a private scope
○ Can declare variables that would be global in the browser
○ Require returns what is assigned to module.exports
var notGlobal;
function func1() {}
function func2() {}
module.exports = {func1: func1, func2: func2};
CS142 Lecture Notes - Node.js
Node modules
● Many standard Node modules
○ File system, process access, networking, timers, devices, crypto, etc.
● Huge library of modules (npm)
○ Do pretty much anything you want
● We use:
○ Express - Fast, unopinionated, minimalist web framework (speak HTTP)
○ Mongoose - Elegant mongodb object modeling (speak to the database)
CS142 Lecture Notes - Node.js
Node Buffer class
● Manipulating lots of binary data wasn't a strength of the JavaScript engine
○ Unfortunately that is what web servers do: DBMS ⇔ Web Server ⇔ Browser
● Node add a Buffer class - Optimized for storing and operating on binary data
○ Interface looks an array of bytes (like the OS system calls use)
○ Memory is allocated outside of the V8 heap
● Used by the wrapped OS I/O calls (fs, net, …)
● Optimized sharing with pointers rather than always copying
○ buffer.copy()
○ For example: fs.readFile to socket.write
CS142 Lecture Notes - Node.js
Buffer operations
● Supports operations for picking values out or updating them
○ Can peek at some values and send the bulk of it on its way
● Can convert a buffer or parts of a buffer to a JavaScript string
○ buf.toString("utf8"); // Convert to UTF8 - commonly used on the web
○ buf.toString("hex"); // Convert to hex encoding (2 digits per byte)
○ buf.toString("base64"); // Convert to base64 encoding
CS142 Lecture Notes - Node.js
Example: Node.js reading a file (redux)
let fs = require("fs");
// fs has 81 properties readFile, writeFile, most OS calls, etc.
fs.readFile("smallFile", readDoneCallback); // Start read
// Read has been launched - JavaScript execution continues
// Node.js exits when no callbacks are outstanding
function readDoneCallback(error, dataBuffer) {
// console.log(dataBuffer) prints <Buffer 66 73 20 3d 20 72 65 71 ...
if (!error) {
console.log("smallFile contents", dataBuffer.toString());
}
}
CS142 Lecture Notes - Node.js
Programming with Events/Callbacks
● Key difference
○ Threads: Blocking/waiting is transparent
○ Events: Blocking/waiting requires callback
● Mental model
If code doesn't block: Same as thread programming
If code does block (or needs to block): Need to setup callback
Often what was a return statement becomes a function call
CS142 Lecture Notes - Node.js
Example: Three step process
Threads
r1 = step1();
console.log('step1 done', r1);
r2 = step2(r1);
console.log('step2 done', r2);
r3 = step3(r2);
console.log('step3 done', r3);
console.log('All Done!');
Works for non-blocking
calls in both styles
Callbacks
step1(function (r1) {
console.log('step1 done', r1);
step2(r1, function (r2) {
console.log('step2 done', r2);
step3(r2, function (r3) {
console.log('step3 done',r3);
});
});
}):
console.log('All Done!'); // Wrong!
CS142 Lecture Notes - Node.js
Example: Three step process
Threads
r1 = step1();
console.log('step1 done', r1);
r2 = step2(r1);
console.log('step2 done', r2);
r3 = step3(r2);
console.log('step3 done', r3);
console.log('All Done!');
Callbacks
step1(function(r1) {
console.log('step1 done', r1);
step2(r1, function (r2) {
console.log('step2 done', r2);
step3(r2, function (r3) {
console.log('step3 done', r3);
console.log('All Done!');
});
});
});
CS142 Lecture Notes - Node.js
Listener/emitter pattern
● When programing with events (rather than threads) a listener/emitter pattern
is often used.
● Listener - Function to be called when the event is signaled
○ Should be familiar from DOM programming (addEventListerner)
● Emitter - Signal that an event has occurred
○ Emit an event cause all the listener functions to be called
CS142 Lecture Notes - Node.js
Node: EventEmitter = require('events');
● Listen with on() and signal with emit()
myEmitter.on('myEvent', function(param1, param2) {
console.log('myEvent occurred with ' + param1 + ' and ' + param2 + '!');
});
myEmitter.emit('myEvent', 'arg1', 'arg2');
● On emit call listeners are called synchronously and in the order the listeners
were registered
● If no listener then emit() is a nop
CS142 Lecture Notes - Node.js
Typical EventEmitter patterns
● Have multiple different events for different state or actions
myEmitter.on('conditionA', doConditionA);
myEmitter.on('conditionB', doConditionB);
myEmitter.on('conditionC', doConditionC);
myEmitter.on('error', handleErrorCondition);
● Handling 'error' is important - Node exits if not caught!
myEmitter.emit('error', new Error('Ouch!'));
CS142 Lecture Notes - Node.js
Streams
● Build modules that produce and/or consume streams of data
● A popular way of structuring servers
○ Network interface ⇔ TCP/IP protocol processing ⇔ HTTP protocol processing ⇔ your code
● Can build connected streams dynamically
○ Add modules on stream: E.g. stream.push(Encryption)
Network interface ⇔ TCP/IP protocol processing ⇔ Encryption ⇔ HTTP processing
● Node's APIs heavily uses streams
○ Readable streams (e.g. fs.createReadStream)
○ Writable stream (e.g. fs.createWriteStream)
○ Duplex stream (e.g. net.createConnection)
○ Transform stream (e.g. zlib, crypto)
CS142 Lecture Notes - Node.js
Readable streams - File reading using streams
var readableStreamEvent = fs.createReadStream("bigFile");
readableStreamEvent.on('data', function (chunkBuffer) { // Could be called multiple times
console.log('got chunk of', chunkBuffer.length, 'bytes');
});
readableStreamEvent.on('end', function() {
// Called after all chunks read
console.log('got all the data');
});
readableStreamEvent.on('error', function (err) {
console.error('got error', err);
});
CS142 Lecture Notes - Node.js
Writable streams - File writing using streams
var writableStreamEvent = fs.createWriteStream('outputFile');
writableStreamEvent.on('finish', function () {
console.log('file has been written!');
});
writableStreamEvent.write('Hello world!n');
writableStreamEvent.end();
// Don't forget writableStreamEvent.on('error', …..
CS142 Lecture Notes - Node.js
Digression: Socket setup for TCP connections
Client (Browser)
sock = socket(AF_INET, SOCK_STREAM, 0);
connect(sock, &serverAddr,
sizeof(serverAddr));
write(sock, "Hi!", 3);
read(sock, buf, 3)
Server (Web Server)
lfd = socket(AF_INET, SOCK_STREAM, 0);
bind(lfd, &serverAddr, sizeof(serveraddr));
listen(lfd, 5);
sock = accept(lfd, &clientaddr, &clientlen);
read(sock, buf, 3);
write(sock, buf, 3)
● TCP/IP socket connection is a reliable, in-order byte stream
○ Note: reads can return data in different chunks that sent
CS142 Lecture Notes - Node.js
TCP Networking on Node.js
● Node net module wraps OS's network routines
● Includes higher level functionality like:
let net = require('net');
net.createServer(processTCPconnection).listen(4000);
Creates a socket, binds port 4000, and listens for connections
Calls function processTCPconnection on each TCP connection
CS142 Lecture Notes - Node.js
Example: A chat server
let clients = []; // List of connected clients
function processTCPconnection(socket) {
clients.push(socket); // Add this client to our connected list
socket.on('data', function (data) {
broadcast( "> " + data, socket); // Send received data to all
});
socket.on('end', function () {
clients.splice(clients.indexOf(socket), 1); // remove socket
});
}
CS142 Lecture Notes - Node.js
Chat Server: broadcast
// Send message to all clients
function broadcast(message, sender) {
clients.forEach(function (client) {
if (client === sender) return; // Don't send it to sender
client.write(message);
});
}
● Our chat server implementation is done!
CS142 Lecture Notes - Node.js
Putting it together: A real simple file server
net.createServer(function (socket) {
socket.on('data', function (fileName) {
fs.readFile(fileName.toString(), function (error, fileData) {
if (!error) {
socket.write(fileData); // Writing a Buffer
} else {
socket.write(error.message); // Writing a String
}
socket.end();
});
});
}).listen(4000);
● Think about concurrency going on here
CS142 Lecture Notes - Node.js
Example: Read three files
● Linux
read(open("f1"), buf1, f1Size);
read(open("f2"), buf2, f2Size);
read(open("f3"), buf3, f3Size);
● Node.js
fs.readFile("f1", function (error, data1) {
fs.readFile("f2", function (error, data2) {
fs.readFile("f3", function (error, data3) {
// Call Pyramid of Doom or Callback Hell
}); }); });
CS142 Lecture Notes - Node.js
Example: Read N files
var fileContents = {};
['f1','f2','f3'].forEach(function (fileName) {
fs.readFile(fileName, function (error, dataBuffer) {
assert(!error);
fileContents[fileName] = dataBuffer;
});
});
If we want to use fileContents how do we know when all reads are finished?
Recall: Can't wait in NodeJS
CS142 Lecture Notes - Node.js
Example: Read N files
var fileContents = {};
['f1','f2','f3'].forEach(function (fileName) {
fs.readFile(fileName, function (error, dataBuffer) {
assert(!error);
fileContents[fileName] = dataBuffer;
if (gotLastCallBack()) allDoneCallback(fileContents);
});
});
● Yuck!
CS142 Lecture Notes - Node.js
Async module: var async = require('async');
● Solution: Write a function that turns waiting into a callback
var fileContents = {};
async.each(['f1','f2','f3'], readIt, function (err) {
if (!err) console.log('Done'); // fileContents filled in
if (err) console.error('Got an error:', err.message);
});
function readIt(fileName, callback) {
fs.readFile(fileName, function (error, dataBuffer) {
fileContents[fileName] = dataBuffer;
callback(error);
});
}
CS142 Lecture Notes - Node.js
Node.JS - many useful built-in modules
● Buffer
● C/C++ Addons
● Child Processes
● Cluster
● Console
● Crypto
● Debugger
● DNS
● Errors
● Events
● File System
● Globals
● Timers
● TLS/SSL
● TTY
● UDP/Datagram
● URL
● Utilities
● V8
● VM
● ZLIB
● HTTP
● HTTPS
● Modules
● Net
● OS
● Path
● Process
● Punycode
● Query Strings
● Readline
● REPL
● Stream
● String Decoder

More Related Content

Similar to NodeJSnodesforfreeinmyworldgipsnndnnd.pdf

Node.js in action
Node.js in actionNode.js in action
Node.js in actionSimon Su
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015Nir Noy
 
New kid on the block node.js
New kid on the block node.jsNew kid on the block node.js
New kid on the block node.jsJoel Divekar
 
How to make a high-quality Node.js app, Nikita Galkin
How to make a high-quality Node.js app, Nikita GalkinHow to make a high-quality Node.js app, Nikita Galkin
How to make a high-quality Node.js app, Nikita GalkinSigma Software
 
Global objects in Node.pdf
Global objects in Node.pdfGlobal objects in Node.pdf
Global objects in Node.pdfSudhanshiBakre1
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applicationsTom Croucher
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudAndrea Righi
 
Building and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsBuilding and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsOhad Kravchick
 
Python twisted
Python twistedPython twisted
Python twistedMahendra M
 
Node.js Course 1 of 2 - Introduction and first steps
Node.js Course 1 of 2 - Introduction and first stepsNode.js Course 1 of 2 - Introduction and first steps
Node.js Course 1 of 2 - Introduction and first stepsManuel Eusebio de Paz Carmona
 
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bChereCheek752
 
Fun Teaching MongoDB New Tricks
Fun Teaching MongoDB New TricksFun Teaching MongoDB New Tricks
Fun Teaching MongoDB New TricksMongoDB
 

Similar to NodeJSnodesforfreeinmyworldgipsnndnnd.pdf (20)

Node.js in action
Node.js in actionNode.js in action
Node.js in action
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015
 
Linux Internals - Part II
Linux Internals - Part IILinux Internals - Part II
Linux Internals - Part II
 
Streams in Node.js
Streams in Node.jsStreams in Node.js
Streams in Node.js
 
New kid on the block node.js
New kid on the block node.jsNew kid on the block node.js
New kid on the block node.js
 
How to make a high-quality Node.js app, Nikita Galkin
How to make a high-quality Node.js app, Nikita GalkinHow to make a high-quality Node.js app, Nikita Galkin
How to make a high-quality Node.js app, Nikita Galkin
 
Global objects in Node.pdf
Global objects in Node.pdfGlobal objects in Node.pdf
Global objects in Node.pdf
 
Book
BookBook
Book
 
MyShell - English
MyShell - EnglishMyShell - English
MyShell - English
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
 
NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloud
 
Nodejs Intro Part One
Nodejs Intro Part OneNodejs Intro Part One
Nodejs Intro Part One
 
Building and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsBuilding and Scaling Node.js Applications
Building and Scaling Node.js Applications
 
Python twisted
Python twistedPython twisted
Python twisted
 
Node.js Course 1 of 2 - Introduction and first steps
Node.js Course 1 of 2 - Introduction and first stepsNode.js Course 1 of 2 - Introduction and first steps
Node.js Course 1 of 2 - Introduction and first steps
 
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
 
Fun Teaching MongoDB New Tricks
Fun Teaching MongoDB New TricksFun Teaching MongoDB New Tricks
Fun Teaching MongoDB New Tricks
 
Getting Input from User
Getting Input from UserGetting Input from User
Getting Input from User
 
Unit-2 Getting Input from User.pptx
Unit-2 Getting Input from User.pptxUnit-2 Getting Input from User.pptx
Unit-2 Getting Input from User.pptx
 

Recently uploaded

VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...Call Girls in Nagpur High Profile
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfParomita Roy
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxjanettecruzeiro1
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceanilsa9823
 
Revit Understanding Reference Planes and Reference lines in Revit for Family ...
Revit Understanding Reference Planes and Reference lines in Revit for Family ...Revit Understanding Reference Planes and Reference lines in Revit for Family ...
Revit Understanding Reference Planes and Reference lines in Revit for Family ...Narsimha murthy
 
Fashion trends before and after covid.pptx
Fashion trends before and after covid.pptxFashion trends before and after covid.pptx
Fashion trends before and after covid.pptxVanshNarang19
 
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiVIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiSuhani Kapoor
 
WAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsWAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsCharles Obaleagbon
 
3D Printing And Designing Final Report.pdf
3D Printing And Designing Final Report.pdf3D Printing And Designing Final Report.pdf
3D Printing And Designing Final Report.pdfSwaraliBorhade
 
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfThe_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfAmirYakdi
 
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...Suhani Kapoor
 
Design Portfolio - 2024 - William Vickery
Design Portfolio - 2024 - William VickeryDesign Portfolio - 2024 - William Vickery
Design Portfolio - 2024 - William VickeryWilliamVickery6
 
MASONRY -Building Technology and Construction
MASONRY -Building Technology and ConstructionMASONRY -Building Technology and Construction
MASONRY -Building Technology and Constructionmbermudez3
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Delhi Call girls
 
Raj Nagar Extension Call Girls 9711199012 WhatsApp No, Delhi Escorts in Raj N...
Raj Nagar Extension Call Girls 9711199012 WhatsApp No, Delhi Escorts in Raj N...Raj Nagar Extension Call Girls 9711199012 WhatsApp No, Delhi Escorts in Raj N...
Raj Nagar Extension Call Girls 9711199012 WhatsApp No, Delhi Escorts in Raj N...ankitnayak356677
 
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 night
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 nightCheap Rate Call girls Malviya Nagar 9205541914 shot 1500 night
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 nightDelhi Call girls
 
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 nightCheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 nightDelhi Call girls
 
Kindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpKindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpmainac1
 
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`dajasot375
 

Recently uploaded (20)

VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
 
Revit Understanding Reference Planes and Reference lines in Revit for Family ...
Revit Understanding Reference Planes and Reference lines in Revit for Family ...Revit Understanding Reference Planes and Reference lines in Revit for Family ...
Revit Understanding Reference Planes and Reference lines in Revit for Family ...
 
Fashion trends before and after covid.pptx
Fashion trends before and after covid.pptxFashion trends before and after covid.pptx
Fashion trends before and after covid.pptx
 
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiVIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
 
WAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsWAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past Questions
 
3D Printing And Designing Final Report.pdf
3D Printing And Designing Final Report.pdf3D Printing And Designing Final Report.pdf
3D Printing And Designing Final Report.pdf
 
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfThe_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
 
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
 
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
 
Design Portfolio - 2024 - William Vickery
Design Portfolio - 2024 - William VickeryDesign Portfolio - 2024 - William Vickery
Design Portfolio - 2024 - William Vickery
 
MASONRY -Building Technology and Construction
MASONRY -Building Technology and ConstructionMASONRY -Building Technology and Construction
MASONRY -Building Technology and Construction
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
 
Raj Nagar Extension Call Girls 9711199012 WhatsApp No, Delhi Escorts in Raj N...
Raj Nagar Extension Call Girls 9711199012 WhatsApp No, Delhi Escorts in Raj N...Raj Nagar Extension Call Girls 9711199012 WhatsApp No, Delhi Escorts in Raj N...
Raj Nagar Extension Call Girls 9711199012 WhatsApp No, Delhi Escorts in Raj N...
 
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 night
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 nightCheap Rate Call girls Malviya Nagar 9205541914 shot 1500 night
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 night
 
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 nightCheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 night
 
Kindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpKindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUp
 
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
 

NodeJSnodesforfreeinmyworldgipsnndnnd.pdf

  • 1. CS142 Lecture Notes - Node.js Node.js Mendel Rosenblum
  • 2. CS142 Lecture Notes - Node.js Threads versus Events request = readRequest(socket); reply = processRequest(request); sendReply(socket, reply); Implementation: Thread switching (i.e. blocking) and a scheduler startRequest(socket); listen("requestAvail", processRequest); listen("processDone", sendReplyToSock); Implementation: Event queue processing
  • 3. CS142 Lecture Notes - Node.js Threads versus Events using Callbacks request = readRequest(socket); reply = processRequest(request); sendReply(socket, reply); Implementation: Thread switching (i.e. blocking) and a scheduler readRequest(socket, function(request) { processRequest(request, function (reply) { sendReply(socket, reply); }); }); Implementation: Event queue processing
  • 4. CS142 Lecture Notes - Node.js Event queue ● Inner loop while (true) { if (!eventQueue.notEmpty()) { eventQueue.pop().call(); } } ● Never wait/block in event handler . Example readRequest(socket); 1. launchReadRequest(socket); // Returns immediately 2. When read finishes: eventQueue.push(readDoneEventHandler);
  • 5. CS142 Lecture Notes - Node.js Node.js ● Take a JavaScript engine from a browser (Chrome's V8 JavaScript Engine) ○ Get same JavaScript on both browser and server ○ Don't need the DOM on the server ● Include DOM-like events and an event queue ○ Everything runs as a call from the event loop (already had one for browser events) ● Make event interface to all OS operations ○ Wrap all the OS blocking calls (file and socket/network io) ○ Add some data handle support ● Add a proper module system (predated import/export) ○ Each module gets its own scope (not everything in window)
  • 6. CS142 Lecture Notes - Node.js Example: Node.js reading a file let fs = require("fs"); // require is a Node module call // fs object wraps OS sync file system calls // OS read() is synchronous but Node's fs.readFile is asynchronous fs.readFile("smallFile", readDoneCallback); // Start read function readDoneCallback(error, dataBuffer) { // Node callback convention: First argument is JavaScript Error object // dataBuffer is a special Node Buffer object if (!error) { console.log("smallFile contents", dataBuffer.toString()); } }
  • 7. CS142 Lecture Notes - Node.js Node Modules ● Import using require() - Can use ES6 import if file name *.mjs ○ System module: require("fs"); // Looks in node_modules directories ○ From a file: require("./XXX.js"); // Reads specified file ○ From a directory: require("./myModule"); // Reads myModule/index.js ● Module files have a private scope ○ Can declare variables that would be global in the browser ○ Require returns what is assigned to module.exports var notGlobal; function func1() {} function func2() {} module.exports = {func1: func1, func2: func2};
  • 8. CS142 Lecture Notes - Node.js Node modules ● Many standard Node modules ○ File system, process access, networking, timers, devices, crypto, etc. ● Huge library of modules (npm) ○ Do pretty much anything you want ● We use: ○ Express - Fast, unopinionated, minimalist web framework (speak HTTP) ○ Mongoose - Elegant mongodb object modeling (speak to the database)
  • 9. CS142 Lecture Notes - Node.js Node Buffer class ● Manipulating lots of binary data wasn't a strength of the JavaScript engine ○ Unfortunately that is what web servers do: DBMS ⇔ Web Server ⇔ Browser ● Node add a Buffer class - Optimized for storing and operating on binary data ○ Interface looks an array of bytes (like the OS system calls use) ○ Memory is allocated outside of the V8 heap ● Used by the wrapped OS I/O calls (fs, net, …) ● Optimized sharing with pointers rather than always copying ○ buffer.copy() ○ For example: fs.readFile to socket.write
  • 10. CS142 Lecture Notes - Node.js Buffer operations ● Supports operations for picking values out or updating them ○ Can peek at some values and send the bulk of it on its way ● Can convert a buffer or parts of a buffer to a JavaScript string ○ buf.toString("utf8"); // Convert to UTF8 - commonly used on the web ○ buf.toString("hex"); // Convert to hex encoding (2 digits per byte) ○ buf.toString("base64"); // Convert to base64 encoding
  • 11. CS142 Lecture Notes - Node.js Example: Node.js reading a file (redux) let fs = require("fs"); // fs has 81 properties readFile, writeFile, most OS calls, etc. fs.readFile("smallFile", readDoneCallback); // Start read // Read has been launched - JavaScript execution continues // Node.js exits when no callbacks are outstanding function readDoneCallback(error, dataBuffer) { // console.log(dataBuffer) prints <Buffer 66 73 20 3d 20 72 65 71 ... if (!error) { console.log("smallFile contents", dataBuffer.toString()); } }
  • 12. CS142 Lecture Notes - Node.js Programming with Events/Callbacks ● Key difference ○ Threads: Blocking/waiting is transparent ○ Events: Blocking/waiting requires callback ● Mental model If code doesn't block: Same as thread programming If code does block (or needs to block): Need to setup callback Often what was a return statement becomes a function call
  • 13. CS142 Lecture Notes - Node.js Example: Three step process Threads r1 = step1(); console.log('step1 done', r1); r2 = step2(r1); console.log('step2 done', r2); r3 = step3(r2); console.log('step3 done', r3); console.log('All Done!'); Works for non-blocking calls in both styles Callbacks step1(function (r1) { console.log('step1 done', r1); step2(r1, function (r2) { console.log('step2 done', r2); step3(r2, function (r3) { console.log('step3 done',r3); }); }); }): console.log('All Done!'); // Wrong!
  • 14. CS142 Lecture Notes - Node.js Example: Three step process Threads r1 = step1(); console.log('step1 done', r1); r2 = step2(r1); console.log('step2 done', r2); r3 = step3(r2); console.log('step3 done', r3); console.log('All Done!'); Callbacks step1(function(r1) { console.log('step1 done', r1); step2(r1, function (r2) { console.log('step2 done', r2); step3(r2, function (r3) { console.log('step3 done', r3); console.log('All Done!'); }); }); });
  • 15. CS142 Lecture Notes - Node.js Listener/emitter pattern ● When programing with events (rather than threads) a listener/emitter pattern is often used. ● Listener - Function to be called when the event is signaled ○ Should be familiar from DOM programming (addEventListerner) ● Emitter - Signal that an event has occurred ○ Emit an event cause all the listener functions to be called
  • 16. CS142 Lecture Notes - Node.js Node: EventEmitter = require('events'); ● Listen with on() and signal with emit() myEmitter.on('myEvent', function(param1, param2) { console.log('myEvent occurred with ' + param1 + ' and ' + param2 + '!'); }); myEmitter.emit('myEvent', 'arg1', 'arg2'); ● On emit call listeners are called synchronously and in the order the listeners were registered ● If no listener then emit() is a nop
  • 17. CS142 Lecture Notes - Node.js Typical EventEmitter patterns ● Have multiple different events for different state or actions myEmitter.on('conditionA', doConditionA); myEmitter.on('conditionB', doConditionB); myEmitter.on('conditionC', doConditionC); myEmitter.on('error', handleErrorCondition); ● Handling 'error' is important - Node exits if not caught! myEmitter.emit('error', new Error('Ouch!'));
  • 18. CS142 Lecture Notes - Node.js Streams ● Build modules that produce and/or consume streams of data ● A popular way of structuring servers ○ Network interface ⇔ TCP/IP protocol processing ⇔ HTTP protocol processing ⇔ your code ● Can build connected streams dynamically ○ Add modules on stream: E.g. stream.push(Encryption) Network interface ⇔ TCP/IP protocol processing ⇔ Encryption ⇔ HTTP processing ● Node's APIs heavily uses streams ○ Readable streams (e.g. fs.createReadStream) ○ Writable stream (e.g. fs.createWriteStream) ○ Duplex stream (e.g. net.createConnection) ○ Transform stream (e.g. zlib, crypto)
  • 19. CS142 Lecture Notes - Node.js Readable streams - File reading using streams var readableStreamEvent = fs.createReadStream("bigFile"); readableStreamEvent.on('data', function (chunkBuffer) { // Could be called multiple times console.log('got chunk of', chunkBuffer.length, 'bytes'); }); readableStreamEvent.on('end', function() { // Called after all chunks read console.log('got all the data'); }); readableStreamEvent.on('error', function (err) { console.error('got error', err); });
  • 20. CS142 Lecture Notes - Node.js Writable streams - File writing using streams var writableStreamEvent = fs.createWriteStream('outputFile'); writableStreamEvent.on('finish', function () { console.log('file has been written!'); }); writableStreamEvent.write('Hello world!n'); writableStreamEvent.end(); // Don't forget writableStreamEvent.on('error', …..
  • 21. CS142 Lecture Notes - Node.js Digression: Socket setup for TCP connections Client (Browser) sock = socket(AF_INET, SOCK_STREAM, 0); connect(sock, &serverAddr, sizeof(serverAddr)); write(sock, "Hi!", 3); read(sock, buf, 3) Server (Web Server) lfd = socket(AF_INET, SOCK_STREAM, 0); bind(lfd, &serverAddr, sizeof(serveraddr)); listen(lfd, 5); sock = accept(lfd, &clientaddr, &clientlen); read(sock, buf, 3); write(sock, buf, 3) ● TCP/IP socket connection is a reliable, in-order byte stream ○ Note: reads can return data in different chunks that sent
  • 22. CS142 Lecture Notes - Node.js TCP Networking on Node.js ● Node net module wraps OS's network routines ● Includes higher level functionality like: let net = require('net'); net.createServer(processTCPconnection).listen(4000); Creates a socket, binds port 4000, and listens for connections Calls function processTCPconnection on each TCP connection
  • 23. CS142 Lecture Notes - Node.js Example: A chat server let clients = []; // List of connected clients function processTCPconnection(socket) { clients.push(socket); // Add this client to our connected list socket.on('data', function (data) { broadcast( "> " + data, socket); // Send received data to all }); socket.on('end', function () { clients.splice(clients.indexOf(socket), 1); // remove socket }); }
  • 24. CS142 Lecture Notes - Node.js Chat Server: broadcast // Send message to all clients function broadcast(message, sender) { clients.forEach(function (client) { if (client === sender) return; // Don't send it to sender client.write(message); }); } ● Our chat server implementation is done!
  • 25. CS142 Lecture Notes - Node.js Putting it together: A real simple file server net.createServer(function (socket) { socket.on('data', function (fileName) { fs.readFile(fileName.toString(), function (error, fileData) { if (!error) { socket.write(fileData); // Writing a Buffer } else { socket.write(error.message); // Writing a String } socket.end(); }); }); }).listen(4000); ● Think about concurrency going on here
  • 26. CS142 Lecture Notes - Node.js Example: Read three files ● Linux read(open("f1"), buf1, f1Size); read(open("f2"), buf2, f2Size); read(open("f3"), buf3, f3Size); ● Node.js fs.readFile("f1", function (error, data1) { fs.readFile("f2", function (error, data2) { fs.readFile("f3", function (error, data3) { // Call Pyramid of Doom or Callback Hell }); }); });
  • 27. CS142 Lecture Notes - Node.js Example: Read N files var fileContents = {}; ['f1','f2','f3'].forEach(function (fileName) { fs.readFile(fileName, function (error, dataBuffer) { assert(!error); fileContents[fileName] = dataBuffer; }); }); If we want to use fileContents how do we know when all reads are finished? Recall: Can't wait in NodeJS
  • 28. CS142 Lecture Notes - Node.js Example: Read N files var fileContents = {}; ['f1','f2','f3'].forEach(function (fileName) { fs.readFile(fileName, function (error, dataBuffer) { assert(!error); fileContents[fileName] = dataBuffer; if (gotLastCallBack()) allDoneCallback(fileContents); }); }); ● Yuck!
  • 29. CS142 Lecture Notes - Node.js Async module: var async = require('async'); ● Solution: Write a function that turns waiting into a callback var fileContents = {}; async.each(['f1','f2','f3'], readIt, function (err) { if (!err) console.log('Done'); // fileContents filled in if (err) console.error('Got an error:', err.message); }); function readIt(fileName, callback) { fs.readFile(fileName, function (error, dataBuffer) { fileContents[fileName] = dataBuffer; callback(error); }); }
  • 30. CS142 Lecture Notes - Node.js Node.JS - many useful built-in modules ● Buffer ● C/C++ Addons ● Child Processes ● Cluster ● Console ● Crypto ● Debugger ● DNS ● Errors ● Events ● File System ● Globals ● Timers ● TLS/SSL ● TTY ● UDP/Datagram ● URL ● Utilities ● V8 ● VM ● ZLIB ● HTTP ● HTTPS ● Modules ● Net ● OS ● Path ● Process ● Punycode ● Query Strings ● Readline ● REPL ● Stream ● String Decoder