Skip to content

devdesai06/URL-Shortener

Repository files navigation

🔗 URL Shortener API

A simple URL Shortener built with Node.js, Express, MongoDB, and Redis, designed to go beyond basic CRUD by adding rate limiting, caching, and unit testing.


🚀 Why this project?

Building a URL shortener is easy. Building one that is scalable, protected, and optimized is not.

This project focuses on real backend concepts like:

  • Preventing abuse using rate limiting
  • Improving performance using Redis caching
  • Writing unit tests for APIs
  • Clean project structure and separation of concerns

✨ Features

  • 🔗 Generate short URLs from long URLs
  • 🔁 Redirect short URLs to original URLs
  • 📊 Basic analytics (visit count)
  • ⚡ Redis-based caching for fast redirects
  • 🛡️ Redis-based rate limiting on API routes
  • 🧪 Unit testing using Jest & Supertest
  • 📦 Clean MVC-based folder structure

🛠️ Tech Stack

  • Node.js
  • Express.js
  • MongoDB + Mongoose
  • Redis
  • Jest & Supertest
  • dotenv

📂 Project Structure

src/
├── controllers/     # Business logic
├── routes/          # API routes
├── models/          # MongoDB schemas
├── middlewares/     # Rate limiting
├── config/          # Redis & DB configs
├── tests/           # API tests
├── app.js
└── server.js

🔑 API Endpoints

Create Short URL

POST /url/getShortUrl

Body:

{
  "longUrl": "https://example.com"
}

Redirect to Original URL

GET /:shortId

Analytics

GET /url/show_data/:shortId

⚡ Redis Usage

🔹 Caching

  • Redirect URLs are cached in Redis
  • Reduces MongoDB queries
  • Faster redirects

🔹 Rate Limiting

  • Applied on /url routes
  • Prevents spam & abuse
  • Uses Redis for distributed rate limiting

🧪 Testing

  • API tests written using Jest
  • Supertest used to test Express endpoints
  • Tests cover:
    • URL creation
    • Error cases
    • API responses

Run tests:

npm test

⚙️ Environment Variables

Create a .env file:

PORT=3000
MONGO_URI=your_mongodb_uri
REDIS_URL=your_redis_url
BASE_URL=http://localhost:3000

▶️ Run Locally

git clone https://github.com/devdesai06/URL-Shortener.git
cd URL-Shortener
npm install
npm start

📌 Future Improvements

  • Expiration for short URLs
  • Custom aliases
  • Authentication & user-based URLs
  • Docker support

Author

Dev Desai
Computer Science Student | Backend & Web Development


⭐ If you found this project helpful, please consider giving it a star on GitHub!

About

URL Shortener app, converts large url into smaller url

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published