Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🔗 LinkSnap - URL Shortener Backend

LinkSnap is a RESTful URL shortener backend built using Node.js, Express.js, MongoDB, and Mongoose. It converts long URLs into short links, redirects users to the original URL, and provides basic click analytics.

🚀 Features

  • Shorten long URLs into unique short links
  • Generate unique short codes
  • Redirect short URLs to original URLs
  • Track number of clicks
  • View analytics for shortened URLs
  • Prevent duplicate URL entries
  • Validate URLs before shortening
  • Retrieve all shortened URLs
  • Global 404 route handling
  • MongoDB database integration

🛠️ Tech Stack

  • Node.js - JavaScript runtime
  • Express.js - Backend web framework
  • MongoDB - NoSQL database
  • Mongoose - MongoDB ODM
  • JavaScript - Programming language
  • Postman - API testing
  • Nodemon - Development server

📌 API Endpoints

Method Endpoint Description
POST /api/urls/shorten Create a short URL
GET /api/urls/:shortCode Redirect to original URL
GET /api/urls/stats/:shortCode Get URL click analytics
GET /api/urls Get all shortened URLs

🔗 Shorten a URL

Request

POST /api/urls/shorten
{
  "originalUrl": "https://www.google.com"
}

Response

{
  "originalUrl": "https://www.google.com",
  "shortUrl": "http://localhost:5000/api/urls/abc123",
  "shortCode": "abc123"
}

📊 URL Analytics

GET /api/urls/stats/abc123

Example response:

{
  "originalUrl": "https://www.google.com",
  "shortCode": "abc123",
  "clicks": 5,
  "createdAt": "2026-07-25T00:00:00.000Z"
}

⚙️ Installation

Clone the repository:

git clone <your-repository-url>
cd linksnap-url-shortener

Install dependencies:

npm install

Create a .env file in the project root:

PORT=5000
MONGO_URI=your_mongodb_connection_string

Start the development server:

npm run dev

Server will run on:

http://localhost:5000

🔄 How It Works

  1. Client sends a long URL to the shortening API.
  2. LinkSnap validates the URL.
  3. The database is checked for an existing URL.
  4. A unique short code is generated for new URLs.
  5. The URL and short code are stored in MongoDB.
  6. Opening the short URL redirects the user to the original URL.
  7. Each redirect increments the click counter.
  8. Analytics can be retrieved using the stats endpoint.

🔒 Environment Variables

Sensitive configuration is stored inside .env and excluded from Git using .gitignore.

node_modules/
.env

👩‍💻 Author

Aditi Rathi

Backend Developer

About

A URL shortener backend built with Node.js, Express.js, MongoDB, and Mongoose featuring URL validation, redirects, click tracking, and analytics.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages