A modern, full-stack URL shortener application with click tracking analytics, built with Node.js, Express, PostgreSQL, and vanilla JavaScript.
- 🎯 URL Shortening: Convert long URLs into short, shareable links
- 📊 Click Analytics: Track click counts and view detailed statistics
- 🛡️ Security: Rate limiting, input validation, and security headers
- 📱 Responsive Design: Clean, mobile-friendly interface
- ⚡ Fast Performance: Optimized database queries and caching
- 🐳 Docker Ready: Easy deployment with Docker and Docker Compose
- Node.js - Runtime environment
- Express.js - Web framework
- PostgreSQL - Primary database
- Prisma - Database ORM and migrations
- Helmet - Security middleware
- express-rate-limit - Rate limiting
- CORS - Cross-origin resource sharing
- Validator.js - Input validation
- nanoid - Unique ID generation
- HTML5 - Markup
- CSS3 - Styling with modern features
- Vanilla JavaScript - Client-side functionality
- Fetch API - HTTP requests
- Docker - Containerization
- Docker Compose - Multi-container orchestration
- Prisma Migrate - Database migrations
url-shortener/
├── backend/
│ ├── src/
│ │ ├── lib/
│ │ │ └── database.js # Database connection
│ │ ├── routes/
│ │ │ └── url.js # URL routes
│ │ └── index.js # Main server file
│ ├── prisma/
│ │ ├── migrations/ # Database migrations
│ │ └── schema.prisma # Database schema
│ ├── package.json
│ ├── .env # Environment variables
│ └── .env.example # Environment template
├── public/
│ ├── index.html # Home page
│ └── stats.html # Statistics page
├── Dockerfile # Docker configuration
├── docker-compose.yml # Docker Compose setup
└── README.md
- Node.js 18+
- PostgreSQL 12+
- npm or yarn
-
Clone the repository
git clone https://github.com/kasadi97/url-shortener.git cd url-shortener -
Install dependencies
cd backend npm install -
Set up environment variables
cp .env.example .env
Edit
.envwith your database configuration:DATABASE_URL="postgresql://username:password@localhost:5432/database_name" NODE_ENV=development PORT=5000
-
Set up database
npx prisma migrate dev
-
Start the server
npm start
-
Open your browser Navigate to
http://localhost:5000
docker-compose up -ddocker build -t url-shortener .
docker run -p 5000:5000 url-shortenerhttp://localhost:5000/api
POST /api/url/shortenRequest Body:
{
"longUrl": "https://example.com/very/long/url"
}Response:
{
"shortCode": "abc123",
"longUrl": "https://example.com/very/long/url",
"message": "URL shortened successfully",
"existing": false
}GET /api/url/stats?page=1&limit=50Response:
{
"data": [
{
"id": 1,
"longUrl": "https://example.com",
"shortCode": "abc123",
"clicks": 42,
"createdAt": "2025-09-22T12:00:00.000Z"
}
],
"pagination": {
"page": 1,
"limit": 50,
"total": 100,
"pages": 2,
"hasNext": true,
"hasPrev": false
},
"summary": {
"totalUrls": 100,
"totalClicks": 1250,
"averageClicks": 12.5
}
}GET /api/url/stats/:shortCodeResponse:
{
"id": 1,
"longUrl": "https://example.com",
"shortCode": "abc123",
"clicks": 42,
"createdAt": "2025-09-22T12:00:00.000Z"
}GET /:shortCodeRedirects to the original URL and increments click counter.
GET /healthResponse:
{
"status": "OK",
"timestamp": "2025-09-22T12:00:00.000Z",
"uptime": 3600,
"environment": "development",
"database": "Connected"
}| Variable | Description | Default |
|---|---|---|
DATABASE_URL |
PostgreSQL connection string | Required |
PORT |
Server port | 5000 |
NODE_ENV |
Environment mode | development |
FRONTEND_URL |
Frontend URL for CORS | http://localhost:5000 |
- Rate Limiting: 100 requests per 15 minutes (general), 20 requests per 15 minutes (URL shortening)
- Input Validation: URL format validation and sanitization
- Security Headers: Helmet.js with CSP
- CORS: Configurable cross-origin policies
# Generate Prisma client
npx prisma generate
# Create and apply migration
npx prisma migrate dev --name migration_name
# Reset database
npx prisma migrate reset
# View database in Prisma Studio
npx prisma studionpm run dev- Clean, focused interface for URL shortening
- Real-time validation and error handling
- Responsive design for all devices
- Comprehensive analytics dashboard
- Summary metrics (total URLs, clicks, averages)
- Detailed URL listing with click counts
- Auto-refresh functionality
- Pagination support
- Connect your GitHub repository
- Set environment variables
- Deploy automatically
- Create new Web Service
- Connect repository
- Set build command:
cd backend && npm install - Set start command:
cd backend && npm start
- Create Heroku app
- Add PostgreSQL addon
- Set environment variables
- Deploy via Git
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License.
Built with ❤️ by kasadi97
