Learning on Web Dev Open is free for all.

Backend Engineering > A server from nothingWhat a server actually is
Phase 05A server from nothing235 of 434

What a server actually is

A process, a port, and a loop that reads bytes off a socket. Everything else in this phase is a convenience on top of that.

Concept14 minAI pair

A server is an ordinary process that asked the operating system for a port and then went to sleep. When bytes arrive on that port the kernel wakes it up, hands over a stream, and the process parses the first line of text, a method, a path, a version, and decides what to do. There is no daemon magic and no special hardware. The same laptop running your editor can do this, which is why localhost works at all.

The parsing is the part frameworks hide. A request arrives as a byte stream that happens to be structured: a request line, headers separated by carriage returns, a blank line, then a body whose length you only know because a header said so, or because the transfer is chunked and you have to read until a terminator. Node hands you this half-parsed, as an event emitter with a URL string and a headers object. Express turns that string into params and a matched route. Neither has done anything you could not do yourself with string manipulation and a lookup table.

Holding that model matters because every confusing thing later, why a body arrives in pieces, why headers cannot be set after the first byte is written, why a hanging request is not an error, is obvious once you remember that you are writing into a socket in order and the client is reading it in order.

You should now be able to

  • Describe what listening on a port means
  • Explain the path from socket bytes to a request object
  • Say what the operating system does and what your code does
Ask the community

Loading…