This is a custom-built HTTP server built using Node.js and TypeScript to understand how HTTP works under the hood — without using any external libraries like Express.js.
- ✅ Manually parses HTTP requests using Node's
netmodule - 🔁 Handles persistent and non-persistent connections (
Connection: keep-alive/close) - 🌍 Supports basic routing:
/→ ReturnsHello World!/echo/:text→ Echoes text back/user-agent→ Returns User-Agent header/files/:filename(GET) → Serves files from a directory/files/:filename(POST) → Saves uploaded file
- 📦 Supports gzip compression if
Accept-Encoding: gzipis provided - 🧠 Manually handles headers, request body, and byte streams
Using net.createServer(), a TCP server is started on localhost:4221. All data is received as raw byte streams.
The server splits requests on \r\n\r\n, separates the request line and headers, and stores them in a JS object.
/– Returns"Hello World!"/echo/:message– Echoes back the message; gzips it ifAccept-Encoding: gzipis sent./user-agent– Returns the value of theUser-Agentheader./files/:filename:- GET – Reads the file from the directory passed with
--directoryflag. - POST – Writes the incoming body to that file path.
- GET – Reads the file from the directory passed with
Content-Lengthis calculated manually.Content-Encodingis set when gzipping.Connectionis honored to keep-alive or close the socket.
Start the server:
ts-node index.ts --directory ./data