Series · updated

A tiny HTTP server in TypeScript

Six chapters from an empty file to a server with routes, middleware, tests and a systemd unit. No framework, Node 24 and nothing else.

  1. A server that answers everything

    1 min

    Chapter 1 of six: an empty folder, Node 24 and a server that says Hello to every request. No dependencies, now or later.

  2. Reading the request

    1 min

    Method, path, headers and body, turned into a standard Request. The server still answers everything with the same text.

  3. A router in 20 lines

    2 min

    Each path gets its own handler, and /posts/:slug hands the slug over as a parameter. None of the 20 lines is a regular expression.

  4. Middleware without a framework

    1 min

    A middleware is a function that takes a handler and returns a handler. Logging and a token check, nested without a framework.

  5. Testing it with node:test

    1 min

    Node ships a test runner. Twelve tests cover the router and the middleware, and none of them opens a port.

  6. Shipping it with a systemd unit

    1 min

    The last chapter puts the server on a real machine: a user of its own, a unit file, logs in journald and a restart when it falls over.

Buy Logbuch