A small but production-minded URL shortener API. It turns a long URL into a short code, redirects visitors to the original, and tracks click analytics along the way. I built it to practice clean REST design, input validation, persistence, rate limiting, and tests, not to reinvent bit.ly.
- Shorten any http or https URL to a compact code
- Optional custom aliases (for example
/launch) with duplicate protection - Optional expiry, after which a link returns
410 Gone - Click analytics: total clicks plus the ten most recent, with referrer and user agent
- Rate limiting on the create endpoint to discourage abuse
- SQLite persistence behind an injectable database, so tests run fully in memory
- A test suite covering the main paths
Node.js and Express, with Node's built-in SQLite (node:sqlite), so there is no native module to compile. Rate limiting uses express-rate-limit, and the tests run on the built-in node --test runner with supertest.
npm install
npm start
# server on http://localhost:3000Create a short link:
curl -X POST http://localhost:3000/api/shorten \
-H "Content-Type: application/json" \
-d '{"url":"https://example.com/a/very/long/path","expiresInDays":30}'{ "code": "Xa3b9Z", "shortUrl": "http://localhost:3000/Xa3b9Z", "expiresAt": 1788900000000 }Follow it, then read its stats:
curl -i http://localhost:3000/Xa3b9Z # 302 redirect to the original URL
curl http://localhost:3000/api/stats/Xa3b9Z| Method | Path | Description |
|---|---|---|
| POST | /api/shorten |
Create a short link |
| GET | /:code |
Redirect to the original URL |
| GET | /api/stats/:code |
Click analytics for a link |
| GET | /api/health |
Health check |
The full contract lives in openapi.yaml.
npm testMIT
