Skip to content

Sahil-4/url-shortener-backend

Repository files navigation

URL Shortener Backend

This is backend part of URL Shortener application created using MERN. We can create a short URL for a long URL and can use short url to reach to the original long URL.

This repository contains code of backed part you can find the github repo of frontend here : Sahil-4/url-shortener You can find the frontned live here : https://a-short.netlify.app/ and backend here : https://a-short.onrender.com/

Features

  • custom otp based email verification : implemented custom logic for email verification while creating a new account.
  • custom base 62 hash generator : implemented custom function to generate unique hash of 6 length to create short url.
  • express : used to create an http/https server.
  • mongoose : to connect to mongodb database and handle db operations.
  • cors : to handle communication between cross origins.
  • dotenv : to keep keys secure.
  • jsonwebtoken : to generate access token.
  • bcrypt : to hash password.
  • nodemailer : to send otp verification email.

API Endpoints

  • POST /api/v1/auth/get-otp: Request to send an OTP for email verification.
  • POST /api/v1/auth/signup: User registration endpoint.
  • POST /api/v1/auth/login: User login endpoint.
  • GET /api/v1/auth/deactivate: Endpoint to delete the user account.
  • GET /api/v1/url/get: Retrieve all URLs generated by a user.
  • POST /api/v1/url/new: Create a new short URL.
  • POST /api/v1/url/delete: Delete a URL.

API Documentation

  • Request OTP POST : /api/v1/auth/get-otp

    body : {
        username : "username",
        email : "example@mail.com",
    }
    
    response : {
        success: true,
        message: "otp send successfully",
        data: null
    }
    
  • Create a new user POST : /api/v1/auth/signup

    body : {
        username : "username",
        email : "example@mail.com",
        password: "password",
        otp: "111111",
    }
    
    response : {
        success: true,
        message: "user created successfully",
        data : {
            _id: user_id,
            username,
            email: user_email,
            token: access_token,
        };
    }
    
  • login POST : /api/v1/auth/login

    body : {
        email : "example@mail.com",
        password: "password",
    }
    
    response : {
        success: true,
        message: "login successful",
        data : {
            _id: user_id,
            username,
            email: user_email,
            token: access_token,
        };
    }
    
  • delete a user GET : /api/v1/auth/deactivate

    response : {
        success: true,
        message: "account deleted successfully",
    }
    
  • Get URLs generated by current logged in user GET : /api/v1/url/get

    response : {
        success: true,
        message: "urls fetched successfully",
        data: [Array of { URL Objects }],
    }
    
  • create a new short URL POST : /api/v1/url/new

    body : {
        original_url : "https://some.valid.url.com/path",
    }
    
    response : {
        success: true,
        message: "url created successfully",
        data: { URL Object },
    }
    
  • Delete a URL POST : /api/v1/url/delete

    body : {
        short_url : "https://a-short.onrender.com/HASH",
    }
    
    response : {
        success: true,
        message: "successfully deleted the url",
        data: { Deleted URL Object }
    }
    
  • Redirect to original URL from short URL GET : /HASH

    RESPONSE : 301, REDIRECT to Original Long URL
    

Installation and Setup

clone the repository

git clone git@github.com:Sahil-4/url-shortener-backend.git

Install required packages

npm install

Add env variables

Create dot env file (.env) in the root directory and add all variables given in .env.example file with proper values in .env file

Start the app

npm run start

to test the application properly make sure to check out frontend part here