Skip to content

A URL shortener developed using Express.js and MongoDB, featuring ad redirection after every 10 hits, a daily limit of 20 requests per URL, and a collision-free mechanism to ensure unique short links.

Notifications You must be signed in to change notification settings

rithvikm007/URL-Shortener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

URL Shortener

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.

Features

  • 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.

Tech Stack

  • Backend: Node.js, Express.js
  • Database: MongoDB
  • Utilities: Environment variables with dotenv, async error handling with wrapAsync

Installation

  1. Clone the repository:

    git clone "https://github.com/rithvikm007/URL-Shortener.git"
    cd URL-Shortener
  2. Install dependencies:

    npm install
  3. 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
  4. Start the server:

    npm start
  5. Access the application at http://localhost:8080.

Endpoints

GET /shorten

Render the page for creating a shortened URL.

POST /shorten

Create a new shortened URL.

  • Request Body:

    {
      "longUrl": "<URL to shorten>"
    }
  • Response:

    {
      "shortUrl": "<Generated short URL>"
    }

GET /show

Render a page displaying all shortened URLs.

GET /redirect/:shortUrl or GET /:shortUrl

Redirect to the original URL for a given short URL.

GET /details/:url

Get details (long URL, short URL, hit counts) for a specific URL.

  • Response:
    {
      "longUrl": "<Original URL>",
      "shortUrl": "<Short URL>",
      "hitCount": <Total hit count>
    }

GET /top/:number

Retrieve the top N most accessed URLs.

  • Response:
    [
      {
        "longUrl": "<Original URL>",
        "shortUrl": "<Short URL>",
        "hitCount": <Hit count>
      }
    ]

Project Structure

├── 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

Future Enhancements

  • 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.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgments

About

A URL shortener developed using Express.js and MongoDB, featuring ad redirection after every 10 hits, a daily limit of 20 requests per URL, and a collision-free mechanism to ensure unique short links.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published