/Shawtyfy

Simple url shortening app

Primary LanguageJavaScriptMIT LicenseMIT

Shawtyfy

This is a simple URL shortening API built using Node.js, Express.js, and MongoDB. It allows users to submit long URLs for shortening and retrieve the original long URL when given the short URL.

Live Webserver: https://shawtify.up.railway.app/

Table of Contents

Features

  • Shorten long URLs to unique, short keys.
  • Redirect users to the original URL when accessing a shortened URL.
  • Store URL mappings in a MongoDB database.
  • Store counts of the number of times a URL has been visited.

Getting Started

Prerequisites

Before you can run the URL Shortener application, make sure you have the following prerequisites installed:

  • Node.js (v18)
  • npm (Node Package Manager)
  • MongoDB (Local or cloud-based instance)

Installation

  1. Clone the repository to your local machine:

    git clone https://github.com/Ahtritus/Shawtyfy.git
    cd url-shortener
  2. Install the project dependencies:

    npm install

Usage

API Endpoints

Shorten a URL

  • URL: /shorten
  • Method: POST
  • Request Body: JSON object with the originalUrl property.
  • Response: JSON object with the shortened URL.

Retrieve Original URL

  • URL: /:shortUrl
  • Method: GET
  • Parameters: shortUrl - The short URL generated by the system.
  • Response: Redirects to the original URL associated with the short URL.

Get All URLs

  • URL: /all
  • Method: GET
  • Response: JSON array containing all currently saved URLs.

Example Usage

  1. Shorten a URL:

    POST /shorten
    Content-Type: application/json
    
    {
      "originalUrl": "https://example.com/my-long-url"
    }

    Response:

    {
      "originalUrl": "https://example.com/my-long-url",
      "shortUrl": "abcdefgh"
    }
  2. Retrieve the Original URL:

    Accessing https://localhost:3000/abcdefgh will redirect you to https://example.com/my-long-url.

  3. Get All URLs:

    GET /all

    Response:

    [
      {
        "originalUrl": "https://example.com/my-long-url",
        "shortUrl": "abcdefgh",
        "visited": 0
      },
      // Other URLs...
    ]

Models

The URL Shortener application uses the following data model for storing URLs:

{
  originalUrl: String,       // The original long URL
  shortUrl: String,          // The shortened URL
  visited: Number,           // Number of times the URL has been visited
}

Configuration

The application uses environment variables for configuration:

  • MONGO_CONNECTION: The MongoDB connection string.

Make sure to set these environment variables before running the application.

Testing

To run tests for the application, execute the following command:

npm test

Deployment

The URL Shortener application can be deployed to a server or cloud hosting platform of your choice. Ensure that you set the required environment variables and configure the MongoDB connection string for the production environment.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please create a GitHub issue or submit a pull request.

License

This project is licensed under the MIT License.