A scalable URL Shortener built with Node.js, Express.js, MongoDB, and NanoID that generates unique short links, performs instant redirection, and tracks click analytics with visit history using a clean MVC architecture.
A high-performance backend application that transforms lengthy URLs into compact, shareable links while tracking usage analytics such as click counts and visit history.
Built using Node.js, Express.js, MongoDB, and NanoID, this project demonstrates the design of a real-world URL shortening service inspired by platforms like Bitly and TinyURL.
Long URLs are difficult to share, visually cluttered, and often contain unnecessary query parameters that reduce readability.
This application solves that problem by generating a unique short identifier for every valid URL using NanoID, storing the mapping inside MongoDB, and instantly redirecting users to the original destination whenever the shortened link is accessed.
Beyond URL shortening, the application records user interactions by maintaining click analytics and visit timestamps, providing useful insights into link usage.
Consider the following URL:
https://www.example.com/products/electronics/mobile/samsung-s25-ultra?campaign=summer-sale&utm_source=newsletter
Problems with such URLs include:
- Difficult to remember
- Hard to share
- Poor readability
- Higher chance of copy errors
- Unsuitable for social media and messaging platforms
Modern applications require shorter, cleaner links that remain reliable while providing insight into how frequently they are used.
The application generates a unique NanoID for every submitted URL.
Each generated identifier is mapped to its corresponding original URL inside MongoDB.
Whenever the shortened URL is accessed:
- The server locates the corresponding document.
- Records the visit timestamp.
- Updates the click analytics.
- Redirects the user to the original destination.
This creates a lightweight yet scalable URL shortening service capable of storing both URL mappings and usage statistics.
- Generate short URLs using NanoID
- Instant HTTP redirection
- URL analytics
- Click count tracking
- Visit timestamp history
- RESTful API design
- Modular MVC architecture
- MongoDB integration using Mongoose
- Organized backend structure
Client
│
▼
Express REST API
│
┌──────────┴──────────┐
▼ ▼
URL Generation URL Redirection
│ │
▼ ▼
Generate NanoID Find Original URL
│ │
▼ ▼
Store in MongoDB Update Analytics
│ │
└──────────┬──────────┘
▼
Redirect User
User submits URL
│
▼
Validate Request
│
▼
Generate NanoID
│
▼
Store URL Mapping
│
▼
Return Short URL
│
▼
User visits Short URL
│
▼
Lookup Original URL
│
▼
Increment Click Count
│
▼
Store Visit Timestamp
│
▼
Redirect User
| Technology | Purpose |
|---|---|
| Node.js | JavaScript runtime |
| Express.js | Backend framework |
| MongoDB | NoSQL database |
| Mongoose | Object Data Modeling (ODM) |
| NanoID | Secure unique short ID generation |
| Git & GitHub | Version control |
URL-SHORTENER/
│
├── controllers/
│ Handles business logic
│
├── models/
│ MongoDB schema definitions
│
├── routes/
│ API endpoint definitions
│
├── connect.js
│ Database connection
│
├── index.js
│ Application entry point
│
├── package.json
│
└── .gitignore
The project follows the MVC (Model-View-Controller) architecture, ensuring a clear separation of concerns and improving maintainability as the application grows.
POST /url
Request
{
"url":"https://www.google.com"
}Returns
{
"id":"abc123"
}GET /:shortId
Redirects the client to the original URL while recording analytics.
GET /analytics/:shortId
Returns
- Original URL
- Short ID
- Total Clicks
- Visit Timestamp History
The application incorporates several design decisions aimed at improving maintainability and efficiency.
Instead of sequential IDs, the application uses NanoID, a secure and collision-resistant identifier generator that produces compact unique IDs suitable for URL shortening.
The codebase follows the MVC pattern, separating routing, business logic, and database operations, making future enhancements easier.
MongoDB stores only the essential URL mapping and analytics information, allowing quick document retrieval and updates during redirection.
Each redirect updates:
- Total click count
- Visit timestamps
This enables usage tracking without requiring complex data processing.
Unlike a basic CRUD application, this project demonstrates concepts commonly found in production backend systems.
It showcases:
- REST API Development
- URL Mapping
- HTTP Redirection
- Backend Routing
- Database Design
- Analytics Collection
- Scalable MVC Architecture
- Secure Unique ID Generation using NanoID
- MongoDB Data Modeling
The project combines backend engineering principles with a practical real-world use case, providing hands-on experience in designing scalable web services.
- Designing collision-resistant short URLs
- Mapping short identifiers to original URLs efficiently
- Recording analytics during every redirect
- Maintaining a clean and modular project structure
- Separating application logic from routing and database operations
Through this project, I gained practical experience in:
- Building REST APIs using Express.js
- Integrating MongoDB with Mongoose
- Designing scalable backend architecture
- Using NanoID for secure identifier generation
- Implementing HTTP redirection
- Tracking application analytics
- Structuring backend projects using MVC principles
Clone the repository
git clone https://github.com/your-username/url-shortener.gitInstall dependencies
npm installStart the server
npm startServer runs on
http://localhost:8000
- ✔ Production-inspired backend architecture
- ✔ Secure short URL generation using NanoID
- ✔ Click analytics with visit history
- ✔ RESTful API implementation
- ✔ Modular MVC design
- ✔ Clean, maintainable codebase
Rahul Kumar
Bachelor of Technology (Computer Science & Engineering)
Passionate about Backend Development, Problem Solving, and Scalable System Design.