Select section

URL Shortener API with Node.js and MongoDB

"A simple yet powerful URL shortening service that allows users to transform long URLs into short, shareable links. Along with URL shortening, this service provides insightful analytics, giving users the ability to track and monitor their shortened URLs' performance."

by Karan Kumar Gautam

02/03/2025

πŸ‘‹πŸŒ

Welcome to this comprehensive guide on building a URL Shortener API using Node.js, Express, and MongoDB. This project demonstrates how to create a RESTful backend for shortening URLs, redirecting to original URLs, and tracking analytics.

Introduction to the URL Shortener API

The URL Shortener API is a backend service that allows users to:

This project is ideal for learning how REST APIs work, managing a MongoDB database using Mongoose, and structuring a scalable Node.js backend.


Project Setup

To run this API on your machine, follow these steps:

1. Clone the Repository

git clone https://github.com/gautamkaran/URL-Shortener-api.git
cd URL-Shortener-api

2. Install Dependencies

npm install

3. Create .env File

Add your MongoDB URI and port in a new .env file:

PORT=3000
MONGODB_URI=mongodb+srv://your-username:your-password@cluster0.lxl3fsq.mongodb.net
CORS_ORIGIN=*

4. Run the Application

npm run dev

The server will start on http://localhost:3000


API Features

βœ‚οΈ URL Shortening

Endpoint: POST /api/shorten

Request Body:

{
  "originalUrl": "https://www.example.com/very/long/url"
}

Response:

{
  "shortUrl": "http://localhost:3000/abc123"
}

πŸ” Redirect to Original URL

Accessing the short URL will redirect you to the original URL.

Example:

GET http://localhost:3000/abc123

πŸ“Š Analytics

Endpoint: GET /api/analytics/:shortId

Response:

{
  "originalUrl": "https://www.example.com/very/long/url",
  "shortUrl": "http://localhost:3000/abc123",
  "clicks": 10
}

Tech Stack


Folder Structure

URL-Shortener-api/
β”‚
β”œβ”€β”€ public/              # Static files
β”œβ”€β”€ src/                 # Source code
β”‚   β”œβ”€β”€ controllers/     # Route controllers
β”‚   β”œβ”€β”€ models/          # Mongoose models
β”‚   β”œβ”€β”€ routes/          # API routes
β”‚   └── utils/           # Utility functions
β”œβ”€β”€ .env                 # Environment variables
β”œβ”€β”€ package.json         # Dependencies
└── README.md

Sample Data Flow

  1. User sends a POST request to /api/shorten

    • The original URL is stored in MongoDB with a generated short ID.
  2. User accesses the short URL

    • The service redirects to the original URL and increments the click count.
  3. User sends a GET request to /api/analytics/:shortId

    • The service returns the original URL, short URL, and total clicks.

Ideal Use Cases


Author

Karan Kumar Gautam
πŸ”— GitHub | 🌐 LinkedIn


License

MIT – Use it freely in your projects


Conclusion

This URL Shortener API project is a great way to get hands-on experience with REST APIs, MongoDB, and Node.js. It’s clean, easy to extend, and ready to be used in real applications.

Happy coding!