A URL shortener with click analytics, written in TypeScript. Create a short link, share it, and see every click it gets — count and last-accessed time.
Shortening a URL is the easy part; the interesting bit is the click tracking: every redirect has to record an event without slowing down the redirect itself, and the storage layer has to hold up under one write per click.
src/domain/ Link, Click, and the short code generator — no I/O
src/repository/ SQLite persistence (one shared connection, WAL mode)
src/service/ LinkService (create/resolve) and StatsService (clicks)
src/http/ Express app, route handlers, error middleware
LinkRepository and ClickRepository both receive an already-open
better-sqlite3 connection instead of opening their own — sharing one
connection avoids write contention on the same database file.
npm install
npm run devThe server starts on :3000 (override with PORT). Data is stored in
data/tinytrail.db (override with DB_PATH).
| Method | Path | Description |
|---|---|---|
| POST | /links |
Create a short link ({ "url": "..." }) |
| GET | /:code |
Redirect to the original URL, records a click |
| GET | /links/:code/stats |
Total clicks and last click time for a link |
curl -X POST localhost:3000/links -H "Content-Type: application/json" \
-d '{"url":"https://example.com"}'
curl -i localhost:3000/<code>
curl localhost:3000/links/<code>/statsnpm test16 tests: unit tests for the short code generator and both services, plus
HTTP integration tests exercising the full create → redirect → stats flow
with supertest.
TypeScript, Express, better-sqlite3 (synchronous SQLite driver — no ORM),
Vitest. Kept intentionally small.