Skip to content

sahuaryan182-tech/url-shortener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🔗 URL Shortener API

A REST API to shorten long URLs, track clicks, and redirect users — built with Express.js and MongoDB.

🛠️ Tech Stack

  • Node.js + Express.js — Server & routing
  • MongoDB + Mongoose — Database
  • nanoid — Short code generation
  • dotenv — Environment variables

🚀 Getting Started

1. Clone the repo

git clone https://github.com/your-username/url-shortener.git
cd url-shortener

2. Install dependencies

npm install

3. Create .env file

PORT=3000
MONGO_URI=mongodb://localhost:27017/urlshortener
BASE_URL=http://localhost:3000

4. Run the server

npm run dev

📬 API Endpoints

Method Endpoint Description
POST /api/url/shorten Shorten a long URL
GET /:shortCode Redirect to original URL
GET /api/url/all Get all shortened URLs
GET /api/url/stats/:shortCode Get click stats
DELETE /api/url/:shortCode Delete a short URL

🧪 Example Usage

Shorten a URL

POST /api/url/shorten
Content-Type: application/json

{
  "originalUrl": "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
}

Response:

{
  "message": "URL shortened successfully",
  "shortUrl": "http://localhost:3000/abc123",
  "data": {
    "originalUrl": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
    "shortCode": "abc123",
    "clicks": 0
  }
}

Get Stats

GET /api/url/stats/abc123

Response:

{
  "originalUrl": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
  "shortUrl": "http://localhost:3000/abc123",
  "clicks": 5,
  "createdAt": "2024-01-01T00:00:00.000Z"
}

📁 Folder Structure

url-shortener/
├── src/
│   ├── config/db.js
│   ├── controllers/urlController.js
│   ├── models/urlModel.js
│   ├── routes/urlRoutes.js
│   ├── middleware/errorHandler.js
│   └── index.js
├── .env
├── .gitignore
├── README.md
└── package.json

About

This project is a URL shortener service built using Node.js, Express, and MongoDB, allowing users to generate short links and redirect them to original URLs.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors