🔗 A simple and efficient URL shortening service built with Node.js, Express, and MongoDB.
- 🔹 Features
- 🔹 Tech Stack
- 🔹 Prerequisites
- 🔹 Code Style
- 🔹 Installation
- 🔹 Usage
- 🔹 API Endpoints
- 🔹 Database Schema (MongoDB)
- 🔹 Project Structure
- 🔹 How it Works
- 🔹 Note
- 🚀 Fast URL shortening service
- 🔄 URL validation before shortening
- 📊 MongoDB for persistent storage
- 🎯 Custom short code generation
- 🔀 Automatic redirection to original URLs
- ⚡ Built with modern ES6+ modules
- 🛠️ Development mode with auto-reload using Nodemon
- Runtime: Node.js
- Framework:
Expressv5.1.0 - Database: MongoDB (via
Mongoosev8.18.3) - Validation:
valid-urlv1.0.9 - Environment Variables:
dotenvv17.2.3 - Dev Tools:
Nodemonv3.1.10
Before you begin, ensure you have the following installed:
- Node.js (v14 or higher)
- npm or yarn
- MongoDB (local instance or MongoDB Atlas account)
This project uses ES6+ modules. Make sure to:
- Use
import/exportsyntax instead ofrequire/module.exports
- Clone the repository:
git clone https://github.com/Amitrajit007/url-shortner
cd URL-SHORTENER- Install dependencies:
npm install- Set up environment variables:
Create a .env file in the root directory:
PORT=5000
MONGO_URI=<your-mongodb-connection-string>
BASE_URL=http://localhost:5000
# Add any other environment variables your app needs- Start the development server:
npm startThe server will start on http://localhost:5000 (or the port specified in your .env file make sure to change the BASE_URL if u change the PORT.).
Send a POST request to create a shortened URL:
curl -X POST http://localhost:5000/url \
-H "Content-Type: application/json" \
-d '{"url": "https://www.example.com/very-long-url"}'Simply navigate to:
http://localhost:5000/{shortCode}
You'll be automatically redirected to the original URL.
Create a new shortened URL.
Request Body:
{
"url": "https://www.example.com/long-url"
}- or
{
"url": "https://www.example.com/long-url",
"expiryDays": 1
}Note: expiryDays: 1 means the URL will expire after 1 day it can be any number.
Response:
{
"success": true,
"data": {
"id": 18,
"originalUrl": "https://www.example.com/long-url",
"shortCode": "i",
"shortUrl": "http://localhost:5000/i",
"clicks": 0,
"expiryDays": 1,
"expiresAt": "2025-10-06T20:48:53.410Z"
}
}Redirect to the original URL associated with the short code.
Special endpoint for testing/Easter egg functionality.
| Field | Type | Description |
|---|---|---|
| id | Number | Auto-increment ID |
| originalUrl | String | Original long URL |
| shortCode | String | Short code (Base62) |
| shortUrl | String | Generated short URL |
| clicks | Number | Number of times URL was clicked |
| expiryDays | Number | Optional: number of days before expiry |
| expiresAt | Date | Calculated expiry date (TTL index) |
| createdAt | Date | Document creation timestamp |
| updatedAt | Date | Document last update timestamp |
url-shortener/
├── server/
│ └── src/
│ ├── app.js # Main application entry point
│ ├── config/
│ │ └── url.config.js # Database configuration
│ ├── controllers/
│ │ ├── shorturlCreate.controller.js # URL shortening logic
│ │ ├── redirect.controller.js # Redirection handling
│ │ └── X.controller.js # Easter egg handling
│ │
│ ├── model/
│ │ └── url.model.js # URL schema and model
│ ├── data/
│ │ └── repository.js # Data access layer
│ ├── routes/
│ │ ├── creatUrl.route.js # URL creation route
│ │ ├── redirect.route.js # Redirection route
│ │ └── X.route.js # Easter egg route
│ └── middlewares/
│ └── validator.middleware.js # URL validation middleware
├── .env # Environment variables (not in repo)
├── .gitignore # Git ignore file
├── nodemon.json # Nodemon configuration
├── package.json # Project dependencies
├── package-lock.json # Locked dependencies
└── README.md # Project documentation
-
User sends a long URL to /url.
-
Backend generates a shortCode and stores it in MongoDB along with optional expiry.
-
MongoDB automatically deletes expired URLs (TTL index on expiresAt).
-
When someone visits /:shortCode, the server redirects to the original URL if it’s valid and not expired.
- Test scripts need to be configured in package.json.
- Do not try to shorten a URL that is already shortened by this service.
- TTL deletion is handled automatically by MongoDB.
- Expired URLs will return HTTP 410 (Gone).
- Clicks are tracked in the
clicksfield in the database.