This document contains lecture notes on Node.js. It discusses how Node.js uses an event-driven, non-blocking I/O model with callbacks instead of threads. It implements this using an event loop that processes callbacks from an event queue. Standard modules like the file system module wrap synchronous OS calls to utilize asynchronous callbacks. Node.js also includes a Buffer class for efficient binary data handling and a module system for reusable code packages.
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It introduces events and an event queue to handle asynchronous and non-blocking operations. Everything runs as a callback from the event loop. Node.js wraps OS operations in modules to make file, network, and other operations non-blocking and event-driven. It also introduces a module system where each module gets its own private scope. Common patterns in Node.js include using the listener/emitter pattern to handle events and building applications with streams of data.
The document summarizes Express.js, a web application framework for Node.js. It describes Express.js as a fast, unopinionated, and minimalist framework that provides a thin layer on top of Node.js functionality. It explains key Express.js concepts like routing, middleware, HTTP requests and responses, and provides code examples of setting up an Express app and defining routes and middleware.
Introducing Node.js in an Oracle technology environment (including hands-on)Lucas Jellema
This presentation introduces Node.js in a few simple, straightforward steps. First, Node.js is presented as just JavaScript on the browser, then HTTP handling is discussed with core module http and subsequently using Express. Running Oracle JET from Node.js is explained. The implementation of APIs - REST services supporting various [operations on] resources is discussed. The single-thread nature of Node.js is presented, along with the essentials of asynchrous programming, working with callbacks and using the async module. The Node Oracle DB Database driver is introduced and demonstrated. Finally, further steps are suggested. This presentation is supported by a set of resources that constitute a three hour hands on session - sources are in GitHub https://github.com/lucasjellema/sig-nodejs-amis-2016.
This document discusses asynchronous I/O in NodeJS. It begins by covering traditional control flow models like single-threaded and multi-threaded processes. It then introduces emerging models like coroutines, fibers, and event-driven non-blocking I/O. The rest of the document focuses on how NodeJS implements an asynchronous event-driven model using libev, libeio, and callbacks. It notes some issues with callbacks and introduces flow control libraries like Steps that help address these issues.
The document discusses the key components and functions of the Unix system kernel. It describes the kernel as managing system resources like CPUs, memory and I/O devices. The major components are the process control subsystem, file subsystem, and hardware control. The kernel handles process management, device management, file management and provides services like virtual memory and networking. It uses a scheduler to allocate CPU time to processes based on their state and priority level.
This document discusses using Node.js in a multi-core environment through clustering. It explains that Node.js is single-threaded, so clustering allows utilizing multiple CPU cores. Approaches include single VM with multiple processes or clustering Node.js processes. Clustering involves a master process that manages worker processes to distribute load. Tests show clustering improves throughput and reduces response times compared to single-core. The cluster module simplifies clustering Node.js across CPU cores.
This document discusses threads and multithreading. It begins with an introduction to threads and their models, including user-level and kernel-level threads. It then covers multithreading approaches like thread-level parallelism and data-level parallelism. The document discusses context switching on single-core versus multicore systems. It also provides an example of implementing matrix multiplication using threads. Finally, it summarizes a case study on using threads in interactive systems.
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It introduces events and an event queue to handle asynchronous and non-blocking operations. Everything runs as a callback from the event loop. Node.js wraps OS operations in modules to make file, network, and other operations non-blocking and event-driven. It also introduces a module system where each module gets its own private scope. Common patterns in Node.js include using the listener/emitter pattern to handle events and building applications with streams of data.
The document summarizes Express.js, a web application framework for Node.js. It describes Express.js as a fast, unopinionated, and minimalist framework that provides a thin layer on top of Node.js functionality. It explains key Express.js concepts like routing, middleware, HTTP requests and responses, and provides code examples of setting up an Express app and defining routes and middleware.
Introducing Node.js in an Oracle technology environment (including hands-on)Lucas Jellema
This presentation introduces Node.js in a few simple, straightforward steps. First, Node.js is presented as just JavaScript on the browser, then HTTP handling is discussed with core module http and subsequently using Express. Running Oracle JET from Node.js is explained. The implementation of APIs - REST services supporting various [operations on] resources is discussed. The single-thread nature of Node.js is presented, along with the essentials of asynchrous programming, working with callbacks and using the async module. The Node Oracle DB Database driver is introduced and demonstrated. Finally, further steps are suggested. This presentation is supported by a set of resources that constitute a three hour hands on session - sources are in GitHub https://github.com/lucasjellema/sig-nodejs-amis-2016.
This document discusses asynchronous I/O in NodeJS. It begins by covering traditional control flow models like single-threaded and multi-threaded processes. It then introduces emerging models like coroutines, fibers, and event-driven non-blocking I/O. The rest of the document focuses on how NodeJS implements an asynchronous event-driven model using libev, libeio, and callbacks. It notes some issues with callbacks and introduces flow control libraries like Steps that help address these issues.
The document discusses the key components and functions of the Unix system kernel. It describes the kernel as managing system resources like CPUs, memory and I/O devices. The major components are the process control subsystem, file subsystem, and hardware control. The kernel handles process management, device management, file management and provides services like virtual memory and networking. It uses a scheduler to allocate CPU time to processes based on their state and priority level.
This document discusses using Node.js in a multi-core environment through clustering. It explains that Node.js is single-threaded, so clustering allows utilizing multiple CPU cores. Approaches include single VM with multiple processes or clustering Node.js processes. Clustering involves a master process that manages worker processes to distribute load. Tests show clustering improves throughput and reduces response times compared to single-core. The cluster module simplifies clustering Node.js across CPU cores.
This document discusses threads and multithreading. It begins with an introduction to threads and their models, including user-level and kernel-level threads. It then covers multithreading approaches like thread-level parallelism and data-level parallelism. The document discusses context switching on single-core versus multicore systems. It also provides an example of implementing matrix multiplication using threads. Finally, it summarizes a case study on using threads in interactive systems.
This document provides an overview of Node.js and how to use it for web development. It covers installing Node.js, the basic syntax and features of Node.js like modules, asynchronous programming. It also discusses using the NPM package manager and popular Node packages. Finally, it demonstrates how to build a basic web server and framework like Express along with integrating a database like MySQL.
My Node.js workshop from Sela's Developer Conference 2015.
In the Workshop we covered The basics Node.js api's and the express web application framework.
The second part of Linux Internals covers system calls, process subsystem and inter process communication mechanisms. Understanding these services provided by Linux are essential for embedded systems engineer.
Streams allow connecting programs like connecting garden hoses. They provide a way to pipe input through multiple steps to an output. There are different types of streams like readable, writable, duplex, and transform streams. Common uses of streams in Node.js include http, fs, child processes, tcp, and zlib. Streams are event emitters that can be used to process data incrementally instead of all at once.
How to make a high-quality Node.js app, Nikita GalkinSigma Software
This document discusses how to build high quality Node.js applications. It covers attributes of quality like understandability, modifiability, portability, reliability, efficiency, usability, and testability. For each attribute, it provides examples of what could go wrong and best practices to achieve that attribute, such as using dependency injection for modifiability, environment variables for portability, and graceful shutdown for reliability. It also discusses Node.js programming paradigms like callbacks, promises, and async/await and recommends best practices for testing Node.js applications.
Global objects in Node.js provide convenient access to commonly used functions and properties without requiring explicit declaration. Some key global objects include __filename and __dirname, which provide file path information, as well as console, process, module, and require, which enable logging, system interaction, and module loading. Additional global functions like setTimeout allow executing code after a delay. TextEncoder and TextDecoder help with text encoding, while URLSearchParams facilitates URL parameter handling. Mastering global objects is important for developing efficient Node.js applications.
The document discusses Node.js and provides instructions for installing Node.js via different methods:
1) Homebrew can be used to install Node.js on OSX by running "brew install node.js".
2) nDistro allows creating and installing Node.js distributions within seconds by specifying module and Node binary version dependencies in a .ndistro file.
3) Node.js can be compiled from source by cloning the Node.js repository via git or downloading the source, running configuration, make, and make install commands.
This paper aims to make students become familiar with the UNIX environment, developing programming skills in C, to increase their exposure to a shell functionality and put into practice concepts of management processes.
This document provides an introduction and overview of a Node.js tutorial presented by Tom Hughes-Croucher. The tutorial covers topics such as building scalable server-side code with JavaScript using Node.js, debugging Node.js applications, using frameworks like Express.js, and best practices for deploying Node.js applications in production environments. The tutorial includes exercises for hands-on learning and demonstrates tools and techniques like Socket.io, clustering, error handling and using Redis with Node.js applications.
This document provides an introduction to NodeJS for beginners. It discusses what NodeJS is, how it uses non-blocking I/O and event-driven architecture, and how to set up NodeJS. It also covers global objects, modules, asynchronous vs synchronous code, core NodeJS modules like filesystem and events, and how to create a basic "Hello World" NodeJS application.
Linux kernel tracing superpowers in the cloudAndrea Righi
The Linux 4.x series introduced a new powerful engine of programmable tracing (BPF) that allows to actually look inside the kernel at runtime. This talk will show you how to exploit this engine in order to debug problems or identify performance bottlenecks in a complex environment like a cloud. This talk will cover the latest Linux superpowers that allow to see what is happening “under the hood” of the Linux kernel at runtime. I will explain how to exploit these “superpowers” to measure and trace complex events at runtime in a cloud environment. For example, we will see how we can measure latency distribution of filesystem I/O, details of storage device operations, like individual block I/O request timeouts, or TCP buffer allocations, investigating stack traces of certain events, identify memory leaks, performance bottlenecks and a whole lot more.
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It uses event-driven, non-blocking I/O model which makes it lightweight and efficient for data-intensive real-time applications that run across distributed devices. Node.js runs in a single thread without blocking, utilizing callbacks and events to handle concurrency. It is commonly used for building fast and scalable network applications.
Python Twisted is an event-driven networking framework. It uses non-blocking functions and callbacks to handle events like data on sockets. Code execution is triggered from an event loop when events occur. This allows for highly scalable servers without threads. Twisted supports many protocols and includes features like deferreds for asynchronous programming and a plugin architecture. However, it is single-threaded and requires external tools to utilize multiple CPUs.
This document outlines an introductory course on learning node.js. It includes an introduction, schedule, today's agenda, explanations of what node.js is and how it is used, essential JavaScript knowledge required, useful development tools, core node.js concepts like events and streams, asynchronous programming, useful libraries, exercises for students, and references for further learning. The goal is to provide students with an understanding of node.js and hands-on experience building simple applications.
MongoDB is the trusted document store we turn to when we have tough data store problems to solve. For this talk we are going to go a little bit off the path and explore what other roles we can fit MongoDB into. Others have discussed how to turn MongoDB’s capped collections into a publish/subscribe server. We stretch that a little further and turn MongoDB into a full fledged broker with both publish/subscribe and queue semantics, and a the ability to mix them. We will provide code and a running demo of the queue producers and consumers. Next we will turn to coordination services: We will explore the fundamental features and show how to implement them using MongoDB as the storage engine. Again we will show the code and demo the coordination of multiple applications.
The document discusses various topics related to Node.js including data types, objects, functions, command line arguments, and getting user input. It provides examples of declaring variables of different data types, creating and accessing objects, defining and calling functions, reading command line arguments, and using modules to get input from the user.
Node handles these tasks by running asynchronously, which means that reading user input from a terminal isn't as simple as calling a getInput() function.
This document provides an overview of Node.js and how to use it for web development. It covers installing Node.js, the basic syntax and features of Node.js like modules, asynchronous programming. It also discusses using the NPM package manager and popular Node packages. Finally, it demonstrates how to build a basic web server and framework like Express along with integrating a database like MySQL.
My Node.js workshop from Sela's Developer Conference 2015.
In the Workshop we covered The basics Node.js api's and the express web application framework.
The second part of Linux Internals covers system calls, process subsystem and inter process communication mechanisms. Understanding these services provided by Linux are essential for embedded systems engineer.
Streams allow connecting programs like connecting garden hoses. They provide a way to pipe input through multiple steps to an output. There are different types of streams like readable, writable, duplex, and transform streams. Common uses of streams in Node.js include http, fs, child processes, tcp, and zlib. Streams are event emitters that can be used to process data incrementally instead of all at once.
How to make a high-quality Node.js app, Nikita GalkinSigma Software
This document discusses how to build high quality Node.js applications. It covers attributes of quality like understandability, modifiability, portability, reliability, efficiency, usability, and testability. For each attribute, it provides examples of what could go wrong and best practices to achieve that attribute, such as using dependency injection for modifiability, environment variables for portability, and graceful shutdown for reliability. It also discusses Node.js programming paradigms like callbacks, promises, and async/await and recommends best practices for testing Node.js applications.
Global objects in Node.js provide convenient access to commonly used functions and properties without requiring explicit declaration. Some key global objects include __filename and __dirname, which provide file path information, as well as console, process, module, and require, which enable logging, system interaction, and module loading. Additional global functions like setTimeout allow executing code after a delay. TextEncoder and TextDecoder help with text encoding, while URLSearchParams facilitates URL parameter handling. Mastering global objects is important for developing efficient Node.js applications.
The document discusses Node.js and provides instructions for installing Node.js via different methods:
1) Homebrew can be used to install Node.js on OSX by running "brew install node.js".
2) nDistro allows creating and installing Node.js distributions within seconds by specifying module and Node binary version dependencies in a .ndistro file.
3) Node.js can be compiled from source by cloning the Node.js repository via git or downloading the source, running configuration, make, and make install commands.
This paper aims to make students become familiar with the UNIX environment, developing programming skills in C, to increase their exposure to a shell functionality and put into practice concepts of management processes.
This document provides an introduction and overview of a Node.js tutorial presented by Tom Hughes-Croucher. The tutorial covers topics such as building scalable server-side code with JavaScript using Node.js, debugging Node.js applications, using frameworks like Express.js, and best practices for deploying Node.js applications in production environments. The tutorial includes exercises for hands-on learning and demonstrates tools and techniques like Socket.io, clustering, error handling and using Redis with Node.js applications.
This document provides an introduction to NodeJS for beginners. It discusses what NodeJS is, how it uses non-blocking I/O and event-driven architecture, and how to set up NodeJS. It also covers global objects, modules, asynchronous vs synchronous code, core NodeJS modules like filesystem and events, and how to create a basic "Hello World" NodeJS application.
Linux kernel tracing superpowers in the cloudAndrea Righi
The Linux 4.x series introduced a new powerful engine of programmable tracing (BPF) that allows to actually look inside the kernel at runtime. This talk will show you how to exploit this engine in order to debug problems or identify performance bottlenecks in a complex environment like a cloud. This talk will cover the latest Linux superpowers that allow to see what is happening “under the hood” of the Linux kernel at runtime. I will explain how to exploit these “superpowers” to measure and trace complex events at runtime in a cloud environment. For example, we will see how we can measure latency distribution of filesystem I/O, details of storage device operations, like individual block I/O request timeouts, or TCP buffer allocations, investigating stack traces of certain events, identify memory leaks, performance bottlenecks and a whole lot more.
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It uses event-driven, non-blocking I/O model which makes it lightweight and efficient for data-intensive real-time applications that run across distributed devices. Node.js runs in a single thread without blocking, utilizing callbacks and events to handle concurrency. It is commonly used for building fast and scalable network applications.
Python Twisted is an event-driven networking framework. It uses non-blocking functions and callbacks to handle events like data on sockets. Code execution is triggered from an event loop when events occur. This allows for highly scalable servers without threads. Twisted supports many protocols and includes features like deferreds for asynchronous programming and a plugin architecture. However, it is single-threaded and requires external tools to utilize multiple CPUs.
This document outlines an introductory course on learning node.js. It includes an introduction, schedule, today's agenda, explanations of what node.js is and how it is used, essential JavaScript knowledge required, useful development tools, core node.js concepts like events and streams, asynchronous programming, useful libraries, exercises for students, and references for further learning. The goal is to provide students with an understanding of node.js and hands-on experience building simple applications.
MongoDB is the trusted document store we turn to when we have tough data store problems to solve. For this talk we are going to go a little bit off the path and explore what other roles we can fit MongoDB into. Others have discussed how to turn MongoDB’s capped collections into a publish/subscribe server. We stretch that a little further and turn MongoDB into a full fledged broker with both publish/subscribe and queue semantics, and a the ability to mix them. We will provide code and a running demo of the queue producers and consumers. Next we will turn to coordination services: We will explore the fundamental features and show how to implement them using MongoDB as the storage engine. Again we will show the code and demo the coordination of multiple applications.
The document discusses various topics related to Node.js including data types, objects, functions, command line arguments, and getting user input. It provides examples of declaring variables of different data types, creating and accessing objects, defining and calling functions, reading command line arguments, and using modules to get input from the user.
Node handles these tasks by running asynchronously, which means that reading user input from a terminal isn't as simple as calling a getInput() function.
Semelhante a NodeJSnodesforfreeinmyworldgipsnndnnd.pdf (20)
Best Digital Marketing Strategy Build Your Online Presence 2024.pptxpavankumarpayexelsol
This presentation provides a comprehensive guide to the best digital marketing strategies for 2024, focusing on enhancing your online presence. Key topics include understanding and targeting your audience, building a user-friendly and mobile-responsive website, leveraging the power of social media platforms, optimizing content for search engines, and using email marketing to foster direct engagement. By adopting these strategies, you can increase brand visibility, drive traffic, generate leads, and ultimately boost sales, ensuring your business thrives in the competitive digital landscape.
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!
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);
});
}