Networking and Parallelism in Node.js

Networking and Parallelism is the fourth chapter of the free Node.js tutorial. It builds web servers without a framework and spreads heavy work over more CPU cores.

What each lesson covers:

  • HTTP and Networking: Sockets and streams, how net, http and tls relate, building an HTTP server with http.createServer, IncomingMessage and ServerResponse, reading the method, URL, headers and body, sending JSON, manual routing, serving files with streams, listen options and server events, HTTP clients with http.request and fetch, keep-alive and agents, server timeouts, why frameworks like Express exist, TCP with net, HTTPS and HTTP/2, UDP, DNS and TLS, and local sockets.
  • Child Processes, Worker Threads and Clustering: When to use parallelism, recognising CPU-bound work, child processes with exec, execFile, spawn and fork, options, streaming output, IPC, killing and detaching processes and handling their errors, worker threads with workerData, parentPort, message passing, structured clone, transferable objects, errors, termination and worker pools, SharedArrayBuffer and Atomics, MessageChannel and resource limits, the cluster module, and choosing between child processes, worker threads and cluster.

Every lab runs a small Node.js runtime in your browser, in JavaScript or TypeScript (pick one in the Language menu at the top). Examples that need the real operating system show output from real Node.js.

Lessons in this section

  1. HTTP and Networking

    Sockets and streams, how net, http and tls relate, building an HTTP server with http.createServer, IncomingMessage and ServerResponse, reading the method, URL, headers and body, sending JSON, manual routing, serving files with streams, listen options and server events, HTTP clients with http.request and fetch, keep-alive and agents, server timeouts, why frameworks like Express exist, TCP with net, HTTPS and HTTP/2, UDP, DNS and TLS, and local sockets.

    • Build an HTTP server with node:http
    • Read the method, URL, headers and body of a request
    • Send JSON and files with the right status codes
    • Call other servers with fetch and set timeouts
  2. Child Processes, Worker Threads and Clustering

    When to use parallelism, recognising CPU-bound work, child processes with exec, execFile, spawn and fork, options, streaming output, IPC, killing and detaching processes and handling their errors, worker threads with workerData, parentPort, message passing, structured clone, transferable objects, errors, termination and worker pools, SharedArrayBuffer and Atomics, MessageChannel and resource limits, the cluster module, and choosing between child processes, worker threads and cluster.

    • Spot CPU-bound work that blocks the event loop
    • Run other programs with exec, execFile, spawn and fork
    • Move heavy JavaScript to worker threads
    • Pass, copy and transfer data between threads