devnotes.

Web Servers, and Where Node.js Fits

Every time you visit a website, there's a program somewhere listening for your request and deciding what to send back. That program is a web server — and understanding what it actually does makes everything else in this chapter click into place.

What happens between a click and a page loading

  1. You type a URL or click a link. Your browser needs to know where to send the request, so it asks a DNS server to translate the domain name into an IP address.
  2. Your browser opens a connection to that address (using TCP, and TLS on top of it for HTTPS).
  3. Your browser sends an HTTP request — a method (GET, POST, etc.), a path, and some headers.
  4. The server receives that request, figures out what's being asked for, does whatever work is needed (look something up, run some logic), and builds a response.
  5. The server sends that response back — a status code, headers, and a body (HTML, JSON, an image, whatever's appropriate).
  6. Your browser renders it.

A "web server" is simply the piece of software running step 4 and 5.

Node.js as a web server

Unlike Apache or Nginx (which are standalone server programs you configure and run), Node.js lets you write the server itself, in code:

const http = require('http');

const server = http.createServer((req, res) => {
  if (req.url === '/health') {
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify({ status: 'healthy' }));
  } else {
    res.writeHead(404);
    res.end('Not found');
  }
});

server.listen(8080, () => console.log('Server ready on port 8080'));

Every decision — which routes exist, what headers to send, how to handle an unknown path — is something you explicitly write, rather than configuring through a separate server program's settings file.

How Node.js compares to a few well-known servers

ServerTypical role
ApacheGeneral-purpose, highly configurable, widely used for traditional websites
NginxExtremely fast at serving static files; commonly used as a reverse proxy in front of other servers
IISMicrosoft's server, built for Windows and ASP.NET applications
Node.jsYou write the server's logic directly in JavaScript; strong for real-time apps and APIs

A pattern you'll see often in production

Many real deployments actually use both: Nginx sits in front, handling incoming traffic, serving static files (images, CSS) directly, and forwarding anything dynamic to a Node.js process running behind it. Node.js handles the application logic; Nginx handles the parts it's traditionally faster and more battle-tested at. Neither replaces the other so much as each covers what it's best suited for.

Web Servers, and Where Node.js Fits | DevNotes