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:
V8andlibuv
Node’s CLI and REPL
- Autocomplete feature
_as last evaluated value.repl commandsreplmodule- Node’s available list commands
node --help | less
“global” Object, “Process”, “Buffer”
- local vs
globalscope processas a bridge between Node app and its running envprocess.versionsprocess.release.ltsprocess.envasPATH- using as configuration
processis an event emitter
Bufferis essentially a used to work with binary streams of data.- read length Buffer.from
- can use similar method as on array, slice
string_decodermodule 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
modulemodule,module.path,module.parentrequire.resolve- algorithm of
requiresearch module.exports=exportsmodule.loaded- examples
JSON and C++ addons
Wrapping and Caching modules
require('module').wrapperrequire.main === modulewith CLI and requiring module printStars.js- delete
require.cacheormodule.exportsas 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
setTimeoutvssetImmediatevsprocess.nextTick, visual explanation [http://stackoverflow.com/questions/17502948/nexttick-vs-setimmediate-visual-explanation#38742776]setImmediateexecutes aftersetTimeout, explanation
Node’s Event-driven architecture
“Callback”, “Promises”, “async/await”
Event Emitter
Arguments, Errors, Order of Listeners
dataevent- handling error with
erroranduncaughtException, register listener withoncemethod, example - order of invoke listeners,
prependListener,removeListener, example - Task List Management example
Node for Networking
TCP networking with
netmodule, example
Working with multiple sockets
Improving the chat server
- remove logging message to ourself, adding names, timestamp, example
The DNS module
lookup,resolve,reversemethods example
UDP sockets
dgrammodule and creating event emitter bydgram.createSocket('udp4'), example
Node for Web
The basic streaming HTTP server
http.createServeras 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
404response- get all response in
http.STATUS_CODES - example
Parsing URL and Query String
- module
urlwithurl.parse('hrefString', ?parseQueryString)andurl.format(), example querystringmodule, 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.inheritsand 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.readFileand 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, exampledetachedoption 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