A small TypeScript + Node.js HTTP server for practicing the fundamentals: routing, request/response, reading request bodies, and writing logs to disk.
GET /echo: returns query-string params as JSON.POST /echo: reads a JSON request body and returns it back as JSON.- CORS enabled (
OPTIONSpreflight supported). - Request logging to
logs/requests.json(NDJSON: one JSON object per line).
- Node.js
>= 18(seepackage.json). - pnpm (optional; repo uses
pnpm-lock.yaml).
pnpm install
If PowerShell blocks pnpm with an execution policy error, run the dev server with npx instead:
npx tsx watch src/server.ts
Development (recommended):
pnpm dev
Build + run:
pnpm buildpnpm start
The server starts on http://localhost:3000 by default.
To change the port:
- Windows PowerShell:
$env:PORT = "4000" - macOS/Linux:
PORT=4000
Example:
curl "http://localhost:3000/echo?name=Emmanuel&topic=node"
Response shape:
{ "message": "...", "query": { ... }, "timestamp": "..." }
Example:
curl -X POST "http://localhost:3000/echo" -H "Content-Type: application/json" -d "{\"hello\":\"world\"}"
Response shape:
{ "message": "...", "data": { ... }, "timestamp": "..." }
- Logs are written to
logs/requests.json. - The format is NDJSON (newline-delimited JSON): every request appends one line.
- Each log entry includes:
timestamp,method,pathname,body(raw body for POST), andhostname.
src/server.ts: the server implementation (routing, request body reading, JSON responses, logging).logs/: created at runtime if missing.dist/: compiled output afterpnpm build.
For a glossary of the Node/HTTP concepts used here, see GLOSSARY.md.