Giter Club home page Giter Club logo

hamidbyte / instant-tiny-url-backend Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 44 KB

This project provides a straightforward solution for converting lengthy URLs into concise, easy-to-share links. Enjoy user-friendly features, quick shortening, and a customizable experience. Boost efficiency and simplify your online interactions with our sleek and secure URL Shortener.

JavaScript 92.62% CSS 1.39% EJS 5.99%
tiny-url url-shortener url-shortener-api mongodb mongodb-atlas nodejs bitly short-url shorten-url shorten-urls

instant-tiny-url-backend's Introduction

Instant Tiny URL Backend

This project provides a straightforward solution for converting lengthy URLs into concise, easy-to-share links. Enjoy user-friendly features, quick shortening, and a customizable experience. Boost efficiency and simplify your online interactions with our sleek and secure URL Shortener.

Frontend Repository

For the corresponding frontend, visit the Instant Tiny URL Frontend.

Prerequisites

Make sure you have the following software installed on your machine:

  • Node.js (version 14 or higher)
  • npm (comes with Node.js)
  • MongoDB (Install locally or use MongoDB Atlas for a cloud-based solution.)
  • Git (optional but recommended)

Getting Started

  1. Clone the Repository:

    git clone https://github.com/HamidByte/Instant-Tiny-URL.git
  2. Navigate to the Project Directory:

    cd Instant-Tiny-URL
  3. Install Dependencies:

    npm install
  4. Configure Environment Variables:

    Create a .env file in the root of the project and set the following variables:

     HOST=http://localhost
     PORT=3000
     MONGO_URI_DEV=your-mongo-uri-for-development
     MONGO_URI_PROD=your-mongo-uri-for-production
  5. Update Configurations:

    Modify the configuration files in the config directory according to your needs.

  6. Update your scripts in package.json for a cross-platform solution:

    If you are working in a Windows environment and using the Command Prompt, you should use the set command to set environment variables.

     "scripts": {
       "start": "nodemon index.js",
       "dev": "set NODE_ENV=development && nodemon index.js",
       "prod": "set NODE_ENV=production && nodemon index.js",
     }

    If you are working in a Unix-like environment (Linux or macOS), you should use the export command to set environment variables.

     "scripts": {
       "start": "nodemon index.js",
       "dev": "export NODE_ENV=development && nodemon index.js",
       "prod": "export NODE_ENV=production && nodemon index.js",
     }

    Make sure to check your environment and use the appropriate command accordingly.

  7. Run the Application:

  • For development:

    npm start

    or

    npm run dev
  • For production:

    npm run prod
  1. Open in Browser:

    Open your web browser and go to http://localhost:3000 (or your specified port).

Endpoints

The URL shortener project provides the following endpoints:

Shorten URL

  • Endpoint: /shortener
  • Method: POST
  • Request Body:
    • longUrl: The long URL that you want to shorten.
  • Response:
    • If the URL is valid and successfully shortened, the API will return the shortened URL details.
    • If the generated shortId already exists in the database, the API will generate a new shortId and return the corresponding details.

Example Request:

curl -X POST http://localhost:3000/shortener -H "Content-Type: application/json" -d '{"longUrl": "https://example.com/"}'

Example Response:

{
  "_id": "some-unique-id",
  "longUrl": "https://example.com/",
  "shortId": "abc123",
  "shortUrl": "http://localhost:3000/abc123",
  "createdAt": "timestamp",
  "updatedAt": "timestamp"
}

Redirect to Long URL

  • Endpoint: /:shortId
  • Method: GET
  • Response:
    • If the shortId exists in the database, the API will redirect to the long URL.
    • If the shortId does not exist, the API will return a 404 error.

Example Request:

curl -L http://localhost:3000/abc123

Note: Replace abc123 in the examples with the actual shortId generated.

URL Stats

  • Endpoint: /stats/:shortId
  • Method: GET
  • Response:
    • Returns JSON information for the given shortId, including long URL, shortId, short URL, creation date, update date, and visit count.

Example Request:

curl http://localhost:3000/stats/abc123

Example Response:

{
  "_id": "some-unique-id",
  "longUrl": "https://example.com/",
  "shortId": "abc123",
  "shortUrl": "http://localhost:3000/abc123",
  "createdAt": "timestamp",
  "updatedAt": "timestamp",
  "visitCount": 10
}

Note: Replace abc123 in the examples with the actual shortId generated.

Check URL Existence

  • Endpoint: /check/:shortId
  • Method: HEAD
  • Description: Checks the existence of a URL based on the provided shortId. Responds with a 204 No Content status if the URL exists or a 404 Not Found status if the URL is not found.
  • Note: This endpoint is useful for pre-checking whether a short URL exists before attempting to redirect.

Example Request:

curl -I http://localhost:3000/check/abc123

Example Response:

HTTP/1.1 204 No Content
HTTP/1.1 404 Not Found

Status Codes:

  • 204 No Content: URL exists.
  • 404 Not Found: URL not found.
  • 500 Internal Server Error: Internal server error during the check.

Note: Replace abc123 in the examples with the actual shortId generated.

instant-tiny-url-backend's People

Contributors

hamidbyte avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.