Skip to content

NeverSpot/URL-shortener-

Repository files navigation

🔗 URL Shortener

Live TypeScript Stack Render ISC License

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


Features

  • 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
  • /health endpoint for uptime monitoring
  • Landing page with a built-in shortener form at /
  • Self-healing: a 5-minute health ping prevents Render cold starts

API

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

Create a short 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

Tech Stack

Layer Technology
Language TypeScript
Framework Express
Database MongoDB (persistent store)
Cache Redis (cache-first lookup)
Validation Zod
DI Container tsyringe
Deploy Render

Request flow

Client
  │
  ├─ POST / ──────────────► Controller → Service → Repository
  │                                                    │
  │                                               [Redis hit?]
  │                                                Yes │ No
  │                                                    │  └─► MongoDB → cache in Redis
  │                                                    │
  └─ GET /:code ──────────► Redirect to longUrl ◄──────┘

Project Structure

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

Quick Start

1. Clone & Install

git clone https://github.com/NeverSpot/URL-shortener-.git
cd URL-shortener-
npm install

2. Configure Environment

Create 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

3. Run

# Build and start
npm run build
npm start

# Development (ts-node / nodemon)
npm run dev

Server runs at http://localhost:3001.


Try It

Check health:

curl https://url-shortener-mhp8.onrender.com/health

Shorten 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.


License

ISC

About

A TypeScript URL shortener API built with Express, MongoDB, and Redis, featuring clean architecture, dependency injection, health monitoring, and a live Render deployment.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors