Skip to content

aklilumengesha/URL-Shortener-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

68 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

URL Shortener

A fast and efficient URL shortener built with Fastify, featuring Redis caching, MongoDB storage, and comprehensive analytics.

Features

  • ⚑ Fast URL shortening with nanoid
  • πŸš€ Redis caching for lightning-fast redirects
  • πŸ“Š Comprehensive analytics (clicks, browsers, date trends)
  • πŸ”’ Rate limiting and CORS support
  • 🎨 Custom alias support
  • πŸ“ Click history tracking
  • πŸ” RESTful API with validation

Tech Stack

  • Fastify - Fast web framework
  • MongoDB - Database
  • Redis - Caching layer
  • Node.js - Runtime

Getting Started

Prerequisites

  • Node.js (v18 or higher)
  • MongoDB (v5 or higher)
  • Redis (optional, for caching)

Installation

  1. Clone the repository
git clone <repository-url>
cd url-shortener
  1. Install dependencies
npm install
  1. Configure environment variables
cp .env.example .env
# Edit .env with your configuration
  1. Start MongoDB and Redis (if using)
# MongoDB
mongod

# Redis (optional)
redis-server
  1. Start the server
npm start

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

Development

Run in development mode with auto-reload:

npm run dev

Usage Examples

Create a short URL

curl -X POST http://localhost:3000/api/urls \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com/very/long/url"}'

Create with custom alias

curl -X POST http://localhost:3000/api/urls \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com", "customAlias": "mylink"}'

Get analytics

curl http://localhost:3000/api/analytics/abc123

Access short URL

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

API Documentation

URL Endpoints

Create Short URL

POST /api/urls
Body: { "url": "https://example.com", "customAlias": "optional" }
Response: { "shortCode", "shortUrl", "originalUrl", "createdAt" }

Get URL Details

GET /api/urls/:code
Response: { "shortCode", "originalUrl", "clicks", "createdAt", "source" }

List All URLs

GET /api/urls?page=1&limit=10
Response: { "urls": [...], "pagination": {...} }

Delete URL

DELETE /api/urls/:code
Response: { "message", "code" }

Redirect

GET /:code
Response: 301 Redirect to original URL

Analytics Endpoints

Overall Analytics

GET /api/analytics
Response: { "totalUrls", "totalClicks", "avgClicksPerUrl", "topUrls", "recentUrls" }

URL-Specific Analytics

GET /api/analytics/:code
Response: { "shortCode", "originalUrl", "totalClicks", "statistics", "topBrowsers", "clicksByDate", "recentClicks" }

Project Structure

url-shortener/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ urls.js          # URL management endpoints
β”‚   β”‚   └── analytics.js     # Analytics endpoints
β”‚   β”œβ”€β”€ plugins/
β”‚   β”‚   β”œβ”€β”€ mongodb.js       # MongoDB connection
β”‚   β”‚   └── redis.js         # Redis connection
β”‚   β”œβ”€β”€ schemas/
β”‚   β”‚   └── url.schema.js    # Request validation schemas
β”‚   └── app.js               # Fastify app configuration
β”œβ”€β”€ server.js                # Entry point
β”œβ”€β”€ .env.example             # Environment variables template
└── package.json

Contributing

Contributions are welcome! This project was built incrementally with 60+ commits to demonstrate Fastify best practices.

License

MIT

About

A fast URL shortener REST API built with Fastify, MongoDB, and Redis caching. Features custom aliases, click analytics, browser tracking, and rate limiting.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors