A modern, full-stack URL shortener application built with React, Node.js, Express, and MongoDB. This application allows users to create shortened URLs, track clicks, and manage their links with user authentication.
- Clean Architecture: MVC with DAO pattern for clear separation of concerns and easy maintenance.
- Secure Authentication: JWT with HTTP-only cookies and bcrypt password hashing.
- Custom URLs & Click Tracking: Create personalized short links and monitor usage.
- Modern Frontend Stack: React, Vite, Tailwind CSS, and TanStack Router for fast, responsive UI.
- State & Data Management: Redux Toolkit and TanStack Query for smooth state and API handling.
- Production-Ready Security: CORS, input validation, sanitization, and secure cookie management.
- Robust Error Handling: Centralized error classes and middleware for clean error management.
- Modular Codebase: Backend and frontend maintained as git submodules for independent development.
- Scalable Database: MongoDB with Mongoose schemas and indexing for optimized performance.
- Developer Friendly: ESLint, Prettier, nodemon, and recommended testing setup for consistent code.
- URL Shortening: Convert long URLs into short, manageable links
- Custom Slugs: Authenticated users can create custom short URLs
- Click Tracking: Monitor how many times each link has been clicked
- User Authentication: JWT-based authentication with secure cookie storage
- User Dashboard: Manage all your shortened URLs in one place
- Responsive Design: Mobile-first design with Tailwind CSS
- Security: HTTP-only cookies, CORS protection, password hashing with bcrypt
- Error Handling: Centralized error handling with custom error classes
- Database: MongoDB with Mongoose ODM and proper indexing
- Validation: Input validation and sanitization
- Middleware: Authentication middleware and user attachment
- Modern Architecture: Clean separation of concerns with MVC pattern
...
Since this repository contains the frontend and backend as git submodules, use the following commands to clone the entire project including submodules:
git clone --recurse-submodules https://github.com/pranavkuruvella/urlshortener.gitIf you have already cloned without submodules, initialize and update them using:
git submodule update --init --recursive- Node.js (v18 or higher)
- MongoDB (local or cloud instance)
- npm or yarn package manager
-
Navigate to backend directory:
cd backend -
Install dependencies:
npm install
-
Environment Configuration: Create a
.envfile in the backend directory:# Database MONGO_URI=URI # Server PORT=3000 NODE_ENV=development # JWT Secret (use a strong secret in production) JWT_SECRET=your-super-secret-jwt-key # Frontend URL (for CORS) FRONTEND_URL=http://localhost:5173
-
Start the backend server:
npm run dev
The backend will be running at
http://localhost:3000
-
Navigate to frontend directory:
cd frontend -
Install dependencies:
npm install
-
Start the development server:
npm run dev
The frontend will be running at
http://localhost:5173
POST /api/auth/register
Content-Type: application/json
{
"name": "John Doe",
"email": "[email protected]",
"password": "securepassword"
}POST /api/auth/login
Content-Type: application/json
{
"email": "[email protected]",
"password": "securepassword"
}GET /api/auth/me
Cookie: accessToken=jwt_token_herePOST /api/create
Content-Type: application/json
Cookie: accessToken=jwt_token_here (optional, for custom slugs)
{
"url": "https://example.com/very-long-url",
"customSlug": "my-custom-slug" // Optional, requires authentication
}GET /api/user/urls
Cookie: accessToken=jwt_token_hereGET /:shortCode
# Redirects to original URL and increments click count-
Authentication & Authorization:
- JWT tokens stored in HTTP-only cookies
- Secure cookie configuration (secure flag in production)
- SameSite cookie protection
- Password hashing with bcrypt (salt rounds: 10)
-
Input Validation & Sanitization:
- URL validation for shortening requests
- Email validation for user registration
- Custom slug validation and sanitization
-
Error Handling:
- Centralized error handling middleware
- Custom error classes (AppError, NotFoundError, ConflictError, etc.)
- Proper error status codes and messages
- Stack trace capture for debugging
-
Database Security:
- Mongoose schema validation
- Indexed fields for performance
- Password field excluded from queries by default
- Unique constraints on email and short URLs
-
CORS Protection:
- Configured CORS for specific frontend origin
- Credentials support for cookie-based authentication
-
Environment Configuration:
- Environment-based configuration
- Secure cookie settings in production
- Database connection error handling with process exit
- Responsive Design: Mobile-first approach with Tailwind CSS
- Modern Interface: Clean, professional design with rounded corners and shadows
- Interactive Elements: Copy-to-clipboard functionality, loading states
- User Feedback: Success/error messages, form validation
- Dark/Light Theme Support: Ready for theme implementation
- Accessibility: Semantic HTML and proper form labels
{
name: String (required),
email: String (required, unique),
password: String (required, select: false),
avatar: String (default: gravatar)
}{
full_url: String (required),
short_url: String (required, unique, indexed),
clicks: Number (default: 0),
user: ObjectId (ref: 'User', optional)
}-
Environment Variables:
NODE_ENV=production MONGO_URI=mongodb+srv://username:[email protected]/urlshortener JWT_SECRET=your-production-jwt-secret-key PORT=3000 FRONTEND_URL=https://yourdomain.com
-
Security Enhancements:
- Enable HTTPS in production
- Use strong JWT secrets
- Implement rate limiting
- Add request logging
- Set up monitoring and alerts
-
Database Optimizations:
- Use MongoDB Atlas or managed MongoDB service
- Enable database indexing
- Set up database backups
- Monitor database performance
-
Build for Production:
npm run build
-
Environment Configuration:
- Update API endpoints to production URLs
- Configure proper CORS settings
- Optimize build settings in vite.config.js
-
Deployment Options:
- Vercel: Automatic deployments from Git
- Netlify: Static site hosting with serverless functions
- AWS S3 + CloudFront: Scalable static hosting
- Docker: Containerized deployment
- Backend Hosting: Railway, Render, or AWS EC2
- Database: MongoDB Atlas (managed)
- Frontend Hosting: Vercel or Netlify
- CDN: CloudFlare for global distribution
- Monitoring: New Relic or DataDog
- Error Tracking: Sentry
- Analytics: Google Analytics or Mixpanel
This project is licensed under the ISC License.
If you encounter any issues or have questions:
- Check the existing issues in the repository
- Create a new issue with detailed information
- Provide steps to reproduce the problem
- Include relevant error messages and logs
Built with β€οΈ using modern web technologies