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.
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
- 🔗 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
- Node.js
- Express.js
- MongoDB + Mongoose
- Redis
- Jest & Supertest
- dotenv
src/
├── controllers/ # Business logic
├── routes/ # API routes
├── models/ # MongoDB schemas
├── middlewares/ # Rate limiting
├── config/ # Redis & DB configs
├── tests/ # API tests
├── app.js
└── server.js
POST /url/getShortUrlBody:
{
"longUrl": "https://example.com"
}GET /:shortIdGET /url/show_data/:shortId- Redirect URLs are cached in Redis
- Reduces MongoDB queries
- Faster redirects
- Applied on
/urlroutes - Prevents spam & abuse
- Uses Redis for distributed rate limiting
- API tests written using Jest
- Supertest used to test Express endpoints
- Tests cover:
- URL creation
- Error cases
- API responses
Run tests:
npm testCreate a .env file:
PORT=3000
MONGO_URI=your_mongodb_uri
REDIS_URL=your_redis_url
BASE_URL=http://localhost:3000git clone https://github.com/devdesai06/URL-Shortener.git
cd URL-Shortener
npm install
npm start- Expiration for short URLs
- Custom aliases
- Authentication & user-based URLs
- Docker support
Dev Desai
Computer Science Student | Backend & Web Development
⭐ If you found this project helpful, please consider giving it a star on GitHub!