6

URL Shortener Project

A simple Node.js-based URL shortener app with MongoDB and Express.js.

πŸ”— URL Shortener Project

A lightweight, efficient URL Shortener application built with Node.js, Express, and MongoDB. This backend-focused project allows users to convert long URLs into short ones and redirect back to the original destinationβ€”showcasing key backend skills such as API development, error handling, and database management.


✨ Features

  • πŸ”— Shorten URLs: Generate unique, shortened links
  • β†ͺ️ Redirection: Automatically redirect short URLs to the original link
  • πŸ“‘ REST API: Clean endpoints for interacting with the application
  • πŸ’Ύ MongoDB Integration: URL mappings stored using Mongoose ODM
  • 🚫 Error Handling: Invalid URLs and non-existent short codes handled gracefully
  • πŸ”§ Modular Design: Easily extendable for auth, analytics, and more

πŸ›  Tech Stack

  • Backend: Node.js, Express.js
  • Database: MongoDB (with Mongoose)
  • Utilities: dotenv, nanoid or shortid for unique code generation

πŸ“ Folder Structure


Url-Shortner/
β”œβ”€β”€ controllers/
β”œβ”€β”€ models/
β”œβ”€β”€ routes/
β”œβ”€β”€ .env
β”œβ”€β”€ app.js
β”œβ”€β”€ package.json
└── README.md

βš™οΈ How It Works

  1. A POST request is made with a long URL
  2. Server generates a short ID and stores the mapping in MongoDB
  3. A GET request with the short code redirects to the original URL

πŸ”„ Sample API Usage

βž• Create Short URL

POST /api/shorten

{
  "originalUrl": "https://www.example.com"
}

Response:

{
  "shortUrl": "https://yourdomain.com/abc123"
}

πŸ” Redirect

GET /:shortCode Redirects user to the original long URL


πŸ§ͺ Running Locally

git clone https://github.com/Sonupandit9693/Learning-Node-js-Project.git
cd Learning-Node-js-Project/Url-Shortner
npm install
  • Create a .env file with:

    MONGODB_URI=your-mongodb-connection-string
    BASE_URL=http://localhost:3000
npm start
  • Test the API using Postman, Thunder Client, or a browser.

πŸ“š What I Learned

  • REST API architecture with Express
  • MongoDB schema modeling and integration
  • Error handling and async programming
  • Environment configuration and scalable folder structures