A modern, efficient URL shortener built with Node.js, Express, MongoDB, and Redis. This application provides fast URL shortening with caching capabilities and analytics tracking.
- 🔗 URL shortening with custom short IDs
- 📊 Analytics tracking for each shortened URL
- ⚡ Redis caching for improved performance
- 🔒 Secure URL handling
- 📱 Responsive frontend design
- 🚀 Easy deployment with Render
- Node.js
- Express.js
- MongoDB (with Mongoose)
- Redis (for caching)
- nanoid (for generating unique IDs)
- HTML5
- CSS3
- JavaScript (ES6+)
- Material Icons
Before you begin, ensure you have the following installed:
- Node.js (v14 or higher)
- MongoDB (local or Atlas)
- Redis (local or cloud service like Upstash)
- npm or yarn
Shortly/
├── BackendApplication/
│ ├── config/
│ │ └── redis.js
│ ├── Controller/
│ │ └── UrlController.js
│ ├── Models/
│ │ └── UrlModel.js
│ ├── Routes/
│ │ └── UrlRoute.js
│ ├── services/
│ │ ├── redisService.js
│ │ └── urlService.js
│ ├── .env
│ ├── .env.example
│ ├── .gitignore
│ ├── package.json
│ └── UrlApplicationServer.js
└── FrontendApplication/
├── index.html
├── styles.css
├── script.js
└── config.js
-
Clone the repository
git clone https://github.com/yourusername/shortly.git cd shortly -
Backend Setup
cd BackendApplication npm install -
Environment Variables Create a
.envfile in the BackendApplication directory:PORT=8080 MONGODB_URI=your_mongodb_connection_string NODE_ENV=development REDIS_URL=your_redis_url
-
Frontend Setup
cd ../FrontendApplication # No installation needed for frontend
-
Start the Application
# Start backend (from BackendApplication directory) npm start # Frontend can be served using any static file server # For example, using Python's built-in server: python -m http.server 3000
- POST
/api/url- Request body:
{ "url": "https://example.com" } - Response:
{ "shortId": "abc123", "originalUrl": "https://example.com", "shortUrl": "http://localhost:8080/abc123" }
- Request body:
- GET
/api/url/analytics/:shortId- Response:
{ "shortId": "abc123", "originalUrl": "https://example.com", "totalClicks": 5, "analytics": [...] }
- Response:
- GET
/:shortId- Redirects to the original URL
The application uses Redis for caching with the following strategy:
- URL shortening results are cached for 24 hours
- Analytics data is cached for 24 hours
- Redirect URLs are cached for 24 hours
- Cache invalidation on URL updates
- Push your code to GitHub
- Create a new Web Service on Render
- Connect your GitHub repository
- Set environment variables in Render dashboard
- Deploy!
- Update the
config.jswith your production API URL - Deploy to any static hosting service (Netlify, Vercel, etc.)
PORT: Server port (default: 8080)MONGODB_URI: MongoDB connection stringNODE_ENV: Environment (development/production)REDIS_URL: Redis connection URL
API_URL: Backend API URL
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- nanoid for generating unique IDs
- MongoDB Atlas for database hosting
- Upstash for Redis hosting
- Render for deployment