A simple and efficient URL shortener API built with Express.js, TypeScript, and SQLite.
- 🔗 URL Shortening: Convert long URLs into short, shareable links
- 📊 Analytics: Track click counts and usage statistics
- 🔄 Smart Redirects: Automatic redirection with click tracking
- 🛡️ Input Validation: Validates URL format before shortening
- 📱 RESTful API: Clean, standard HTTP endpoints
- ⚡ Fast Lookups: SQLite database for quick URL resolution
- 🔧 Environment Config: Configurable port and base URL
- Express.js - Web framework
- TypeScript - Type safety
- SQLite3 - Lightweight database
- nanoid - Short code generation
- ESLint + Prettier - Code quality
- Node.js (v18 or higher)
- pnpm (recommended) or npm
- Clone the repository:
git clone <repository-url>
cd be
- Install dependencies:
pnpm install
- Start the development server:
pnpm dev
The server will start at http://localhost:3000
POST /shorten
Content-Type: application/json
{"url": "https://www.google.com"}
# Response:
{
"shortCode": "abc123",
"shortUrl": "http://localhost:3000/abc123",
"originalUrl": "https://www.google.com",
"clickCount": 0,
"createdAt": "2024-01-01T00:00:00.000Z"
}
GET /abc123
# → Redirects to https://www.google.com
# → Increments click count
GET /api/urls
# Returns list of all shortened URLs with stats
GET /api/stats/abc123
# Returns detailed stats for specific short code
DELETE /api/urls/abc123
# Removes the short URL from database
Create a .env
file based on env.example
:
PORT=3000
BASE_URL=http://localhost:3000
DATABASE_PATH=./urls.db
SHORT_CODE_LENGTH=4
pnpm dev
- Start development serverpnpm lint
- Check for linting errorspnpm lint:fix
- Auto-fix linting errorspnpm format
- Format code with Prettierpnpm format:check
- Check if code is formatted
urls (
id INTEGER PRIMARY KEY AUTOINCREMENT,
shortCode TEXT UNIQUE, -- 4-character short code
originalUrl TEXT, -- Full original URL
createdAt DATETIME, -- Creation timestamp
clickCount INTEGER, -- Number of redirects
lastClickedAt DATETIME -- Last redirect time
)
ISC