This is a simple URL shortener application built using Node.js, Express, MongoDB, and EJS for the frontend. It allows users to generate shortened URLs, track URL analytics, and manage shortened links efficiently.
- Generate shortened URLs for long URLs.
- Option to provide a custom alias.
- Store shortened URLs in MongoDB.
- Track analytics such as total clicks and visit history.
- Implement rate limiting to prevent abuse.
- Error handling and logging for better debugging.
- Backend: Node.js, Express.js
- Database: MongoDB with Mongoose ORM
- Frontend: HTML, CSS, JavaScript, EJS
- Middleware: Express-rate-limit, Winston for logging
-
Clone the repository:
git clone https://github.com/your-username/url-shortener.git cd url-shortener -
Install dependencies:
npm install
-
Set up environment variables: Create a
.envfile in the root directory and configure the following:PORT=8001 MONGO_URI=mongodb://localhost:27017/urlShortener
-
Start the server:
npm start
Endpoint: POST /url
Request Body:
{
"longUrl": "https://example.com",
"customAlias": "myCustomAlias" // (Optional)
}Response:
{
"shortUrl": "http://localhost:8001/urls/myCustomAlias"
}Endpoint: GET /url/analytics/:shortId
Response:
{
"totalClicks": 10,
"analytics": [
{
"timeStamp": "2024-03-23T12:00:00Z",
"ip": "192.168.1.1",
"userAgent": "Mozilla/5.0"
}
]
}url-shortener/
│-- controllers/
│ ├── url.js # Handles URL generation & analytics
│-- middlewares/
│ ├── errorHandler.js # Handles errors
│ ├── logger.js # Logs errors and info
│ ├── rateLimiter.js # Limits API requests
│-- models/
│ ├── url.js # Mongoose schema for URLs
│-- public/
│ ├── scripts.js # Frontend interactions
│ ├── styles.css # Styling
│-- routes/
│ ├── staticRouter.js # Serves home page
│ ├── url.js # URL-related routes
│-- views/
│ ├── home.ejs # Main frontend view
│-- .env # Environment variables
│-- index.js # Main server file
│-- connect.js # Connect mongoose schema
│-- package.json # Project dependencies
│-- README.md # Project documentation
- Rate Limiting: Limits users to 10 requests per minute.
- Error Handling: Centralized error handler for better debugging.
- Logging: Logs errors and important info using Winston.
- Add authentication for URL management.
- Implement a user dashboard to view and manage URLs.
- Custom expiration for shortened URLs.
- Deploy to a cloud provider (Heroku, Vercel, or AWS).
This project is open-source and available under the MIT License.
Atul Kumar Kanojia