A URL shortener backend built with Node.js, Express, and MongoDB. This project enables users to shorten URLs, track usage statistics, enforce daily request limits, and more.
- Shorten URLs: Convert long URLs into short, shareable links.
- Redirection: Automatically redirect to the original URL when a short link is accessed.
- Daily Request Limits: Enforce a configurable daily request limit for individual short URLs. (Note: The daily hit count resets every day at UTC.)
- Analytics:
- Total and daily hit counts for each URL.
- Track the most accessed URLs.
- Ad Redirection: Redirect to an ad every 10th visit to a short URL.
- Top URLs: Retrieve the top N most accessed URLs.
- URL Details: View detailed statistics for any URL.
- Backend: Node.js, Express.js
- Database: MongoDB
- Utilities: Environment variables with
dotenv
, async error handling withwrapAsync
-
Clone the repository:
git clone "https://github.com/rithvikm007/URL-Shortener.git" cd URL-Shortener
-
Install dependencies:
npm install
-
Create a
.env
file in the root directory and configure the following variables:BACKEND_URL=http://localhost:8080 # Can adjust the port by changing its value in app.js DAILY_REQUEST_LIMIT=100 # Adjust as needed MONGO_URI=mongodb://localhost:27017/urlShortener SESSION_SECRET=your_secret_key # Secret key for session management
-
Start the server:
npm start
-
Access the application at http://localhost:8080.
Render the page for creating a shortened URL.
Create a new shortened URL.
-
Request Body:
{ "longUrl": "<URL to shorten>" }
-
Response:
{ "shortUrl": "<Generated short URL>" }
Render a page displaying all shortened URLs.
Redirect to the original URL for a given short URL.
Get details (long URL, short URL, hit counts) for a specific URL.
- Response:
{ "longUrl": "<Original URL>", "shortUrl": "<Short URL>", "hitCount": <Total hit count> }
Retrieve the top N most accessed URLs.
- Response:
[ { "longUrl": "<Original URL>", "shortUrl": "<Short URL>", "hitCount": <Hit count> } ]
├── models/
│ └── url.js # Mongoose schema for URL data
├── routes/
│ └── urls.js # All URL-related routes
├── utils/
│ ├── generateShortUrl.js # Logic for generating unique short URLs
│ └── wrapAsync.js # Async error handling utility
├── views/
│ ├── url/ # Templates for URL pages
├── app.js # Express app setup
├── package.json # Dependencies and scripts
├── .env # Environment variables
└── README.md # Project documentation
- User Authentication: Allow users to manage their own shortened URLs.
- Custom Short URLs: Enable users to specify custom short URLs.
- Admin Dashboard: Provide an interface to monitor and manage URLs.
- Advanced Analytics: Include geolocation and device-based usage stats.
This project is licensed under the MIT License. See the LICENSE
file for details.