A simple URL Shortener built with Node.js, Express, and MongoDB. This application allows users to shorten URLs, track visits, and manage a blacklist of domains.
- Shorten URLs and generate a unique short link.
- Redirect to original URL using the short link.
- Track analytics (visit count and history) for each shortened URL.
- Blacklist certain domains to prevent them from being shortened.
- Rate limiting to prevent spam.
git clone https://github.com/shubhamch95/url-shortener.git
cd url-shortenernpm installCreate a .env file in the root directory and add:
MONGO_URI=mongodb://localhost:27017/url-shortener
PORT=3000npm startServer runs on http://localhost:3000
POST /shorten
{
"originalUrl": "https://example.com"
}Response:
{
"shortUrl": "abc123"
}GET /:shortUrl
- Redirects to the original URL.
GET /analytics/:shortUrl Response:
{
"originalUrl": "https://example.com",
"shortUrl": "abc123",
"visitCount": 5,
"visitHistory": ["2024-03-21T10:00:00Z"]
}POST /blacklist
{
"domain": "https://malicious.com"
}Response:
{
"message": "Domain added to blacklist.",
"domain": "malicious.com"
}GET /blacklist Response:
[
{ "domain": "malicious.com", "createdAt": "2024-03-21T10:00:00Z" }
]url-shortener/
│── models/
│ ├── Blacklist.js # Schema for blacklisted domains
│ ├── URL.js # Schema for shortened URLs
│── server.js # Main server file
│── .env # Environment variables
│── .gitignore # Ignored files (node_modules, .env)
│── package.json # Project dependencies
│── README.md # Project documentation
- Node.js - Backend runtime
- Express.js - Web framework
- MongoDB - NoSQL database
- Mongoose - ODM for MongoDB
- shortid - Generate unique short URLs
- dotenv - Manage environment variables
- express-rate-limit - Prevent abuse
This project is licensed under the MIT License.
Shubham Chaudhary
- GitHub: shubhamch95
Feel free to contribute or report issues!
🚀 Happy Coding!