Advanced NodeJS

Published 2017-05-02

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 and libuv

Node’s CLI and REPL

  • Autocomplete feature
  • _ as last evaluated value
  • . repl commands
  • repl 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 env
  • 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 or module.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”

Node’s Event-driven architecture

“Callback”, “Promises”, “async/await”

  • Async != Callback
  • async callback example
  • async promise and async/await example

Event Emitter

Arguments, Errors, Order of Listeners

  • data event
  • handling error with error and uncaughtException, register listener with once method, example
  • order of invoke listeners, prependListener, removeListener, example
  • Task List Management example

Node for Networking

TCP networking with

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 by dgram.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() and res.end()
  • redirect response
  • response with JSON
  • 404 response
  • get all response in http.STATUS_CODES
  • example

Parsing URL and Query String

  • module url with url.parse('hrefString', ?parseQueryString) and url.format(), example
  • querystring module, example

Node’s Common built-in modules

Working with operation system

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 ES6 extends, 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 stream fs.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

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() vs spawn()
  • exec() with options shell, cwd, env, example
  • detached option and childProcess.unref() example with timer.js

The fork() function

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

Save my day