A production-ready URL shortener API written in TypeScript. Short codes are generated and persisted in MongoDB; Redis sits in front for cache-first lookups. The project follows a layered architecture with dependency injection via tsyringe.
Live: https://url-shortener-mhp8.onrender.com
- Shorten any valid URL to a 5-character code
- Cache-first reads: Redis checked before hitting MongoDB
- Input validation with Zod
- Dependency injection via tsyringe
/healthendpoint for uptime monitoring- Landing page with a built-in shortener form at
/ - Self-healing: a 5-minute health ping prevents Render cold starts
| Method | Route | Description |
|---|---|---|
GET |
/ |
Landing page with shortener form |
GET |
/health |
Service health check |
POST |
/ |
Create a short URL |
GET |
/:shortCode |
Redirect to the original URL |
Request:
POST /
Content-Type: application/json
{
"longUrl": "https://example.com/some/very/long/path"
}Response 201:
{
"shortUrl": "aB3xZ"
}Status codes:
| Code | Meaning |
|---|---|
201 |
Short URL created |
422 |
Validation failed (invalid URL) |
404 |
Short code not found |
500 |
Internal server error |
| Layer | Technology |
|---|---|
| Language | TypeScript |
| Framework | Express |
| Database | MongoDB (persistent store) |
| Cache | Redis (cache-first lookup) |
| Validation | Zod |
| DI Container | tsyringe |
| Deploy | Render |
Client
│
├─ POST / ──────────────► Controller → Service → Repository
│ │
│ [Redis hit?]
│ Yes │ No
│ │ └─► MongoDB → cache in Redis
│ │
└─ GET /:code ──────────► Redirect to longUrl ◄──────┘
Controllers/ Request handling and input validation
Services/ Business logic
Repositories/ MongoDB and Redis data access
Interfaces/ Contracts and type definitions
Models/ Mongoose schemas
Routes/ Express route definitions
config/ Database and cache initialization
container.js tsyringe dependency injection setup
index.ts App entry point
git clone https://github.com/NeverSpot/URL-shortener-.git
cd URL-shortener-
npm installCreate a .env file in the project root:
PORT=3001
MONGO_URI=mongodb://localhost:27017
DB_NAME=url_shortener
REDIS_USERNAME=
REDIS_PASSWORD=
REDIS_HOST=127.0.0.1
REDIS_PORT=6379# Build and start
npm run build
npm start
# Development (ts-node / nodemon)
npm run devServer runs at http://localhost:3001.
Check health:
curl https://url-shortener-mhp8.onrender.com/healthShorten a URL:
curl -X POST https://url-shortener-mhp8.onrender.com/ \
-H "Content-Type: application/json" \
-d '{"longUrl": "https://example.com/some/long/path"}'Visit https://url-shortener-mhp8.onrender.com/<shortCode> to be redirected.