This course will teach you the core Node.js concepts and API modules from simple utility modules all the way to streams and clusters.
Node != Javascript
Node’s architecture
- Node VMs:
V8
,Chakra
- V8 Feature Groups:
shipping
,staged (--harmony)
,inprogress (--harmony_for_in)
- All V8 options
node --v8-options | less
- Node’s architecture diagram:
V8
andlibuv
Node’s CLI and REPL
- Autocomplete feature
_
as last evaluated value.
repl commandsrepl
module- Node’s available list commands
node --help | less
“global” Object, “Process”, “Buffer”
- local vs
global
scope process
as a bridge between Node app and its running envprocess.versions
process.release.lts
process.env
asPATH
- using as configuration
process
is an event emitter
Buffer
is essentially a used to work with binary streams of data.- read length Buffer.from
- can use similar method as on array, slice
string_decoder
module provides an API for decoding Buffer objects into strings in a manner that preserves encoded multi-byte UTF-8 and UTF-16 characters, [example]()
How “require” actually works
- Steps: resolving -> loading -> wrapping -> evaluating -> caching
module
module,module.path
,module.parent
require.resolve
- algorithm of
require
search module.exports
=exports
module.loaded
- examples
JSON and C++ addons
Wrapping and Caching modules
require('module').wrapper
require.main === module
with CLI and requiring module printStars.js- delete
require.cache
ormodule.exports
as a function
Know your npm
Concurrency model and Event Loop
What is I/O anyway?
- the definition
- node architecture in I/O
- handling slow I/O operation: synchronous,
fork()
for new process, threads (problem with sharing resources), event loop
The Event Loop
- the definition
- the visualization
The Call Stack
- the definition
- the visualization
Handling slow operation
How callbacks actually work
- the visualization
“setTimeout”, “setImmediate”, “process.nextTick”
- example how to handle async error
setTimeout
vssetImmediate
vsprocess.nextTick
, visual explanation [http://stackoverflow.com/questions/17502948/nexttick-vs-setimmediate-visual-explanation#38742776]setImmediate
executes aftersetTimeout
, explanation
Node’s Event-driven architecture
“Callback”, “Promises”, “async/await”
Event Emitter
Arguments, Errors, Order of Listeners
data
event- handling error with
error
anduncaughtException
, register listener withonce
method, example - order of invoke listeners,
prependListener
,removeListener
, example - Task List Management example
Node for Networking
TCP networking with
net
module, example
Working with multiple sockets
Improving the chat server
- remove logging message to ourself, adding names, timestamp, example
The DNS module
lookup
,resolve
,reverse
methods example
UDP sockets
dgram
module and creating event emitter bydgram.createSocket('udp4')
, example
Node for Web
The basic streaming HTTP server
http.createServer
as an event emitter, example
Working with HTTPS
- create key and certificate with
openssl
- working with
https
, example
Requesting HTTP/HTTPS data
Working with Routes
- read requested url with
http.IncomingMessage
<-req.url
- response pages with
http.ServerResponse
<-res.writeHeader()
andres.end()
- redirect response
- response with JSON
404
response- get all response in
http.STATUS_CODES
- example
Parsing URL and Query String
- module
url
withurl.parse('hrefString', ?parseQueryString)
andurl.format()
, example querystring
module, example
Node’s Common built-in modules
Working with operation system
- module
os
, example
Working with File System
- Task 1: script to fix files in directory. Each file has its data duplicated. Truncate each file in half. solution
- Task 2: scripts to generate/clean old files in a directory. Anything older than 7 days should be deleted solution
- Task 3: watch a directory and report events which were occurred: added, removed, changed solution
Console and Utilites
console.Console
,util.debuglog
,util.deprecate
,util.inherits
and ES6extends
, examples
Debugging
node debug script.js
, commands:help
,restart
,sb(line)
,repl
,watch(var)
,list(lines)
node --inspect-brk script.js
Working with streams
Streams all the thing!
- The definition
- Distinguish to serve enormous file with buffer
fs.readFile
and streamfs.createReadStream
, example
Steam 101
- Types of stream
- All stream are instance of EventEmitter
- Consuming streams
readableStream.pipe(writableStream)
/ events - Stream Events Table
- Readable stream mode: “paused/pull”, “flowing/push”
Implementing Readable and Writable streams
Duplex and Transform streams
- duplex example
- transform example
- transform with gzip example and unzip
Cluster adn Child Process
Scalling Node.js application
- Why one process in one CPU is not enough
- Using multiple process is only way to scale
- Scalability strategies:
- “Cloning”
- “Decomposing (associated with term microservices)”
- “Splitting (sharding)”
Child processes events and standard IO
- 4 different way to create child process:
spawn()
,fork()
,exec()
,execFile()
spawn()
child process example- using
spawn()
as an stream example
The Shell syntax with exec()
and execFile()
exec()
vsspawn()
exec()
with optionsshell, cwd, env
, exampledetached
option andchildProcess.unref()
example with timer.js
The fork()
function
fork()
vsspawn()
- communication with parent and child
- long running process with http request: server and computation
The Cluster module
- Using as a Load Balancer
- Diagram with Master Process and Cloning Process
Load-balancing an HTTP server
- benchmark with request per seconds
- clustering HTTP server example
Broadcasting messages to each Worker
- mocking fetch user from DB only from Master Worker example
Availability and Zero-downtime restart
Shared State and Sticky Load Balancer
- Why shared states with different workers is a problem?
- Sticky Load Balancer as a solution