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.
- Chapters
- Reading time
- Read
-
A server that answers everything
1 minChapter 1 of six: an empty folder, Node 24 and a server that says Hello to every request. No dependencies, now or later.
-
Reading the request
1 minMethod, path, headers and body, turned into a standard Request. The server still answers everything with the same text.
-
A router in 20 lines
2 minEach path gets its own handler, and /posts/:slug hands the slug over as a parameter. None of the 20 lines is a regular expression.
-
Middleware without a framework
1 minA middleware is a function that takes a handler and returns a handler. Logging and a token check, nested without a framework.
-
Testing it with node:test
1 minNode ships a test runner. Twelve tests cover the router and the middleware, and none of them opens a port.
-
Shipping it with a systemd unit
1 minThe 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.