Hey there! This is a simple URL shortener service built with NestJS and MongoDB. Think of it like bit.ly or tinyurl.com, but one you can run yourself.
This service is designed to be cloud-ready and can be easily deployed to AWS Elastic Beanstalk with DocumentDB (MongoDB-compatible) for a fully managed, scalable solution. Whether you want to run it locally with Docker or deploy it to production on AWS, I've got you covered!
Ever had a really long URL that you wanted to share but it looked ugly? This service takes those long URLs and gives you a short, clean link instead. Perfect for social media, emails, or anywhere you want to keep things tidy.
Example:
- Long URL:
https://www.example.com/some/really/long/path/with/lots/of/parameters?param1=value1¶m2=value2
- Short URL:
http://localhost:3000/abc123
- ✅ Shorten any URL instantly
- ✅ Custom short codes (want "mylink" instead of random letters? You got it!)
- ✅ Click tracking (see how many people clicked your link)
- ✅ URL expiration dates (links that automatically stop working after a certain date)
- ✅ Enable/disable links without deleting them
- ✅ Simple REST API that's easy to use
The easiest way to get started is with Docker. Just run:
docker-compose up
That's it! The service will be running at http://localhost:3000
curl -X POST http://localhost:3000/api/url \
-H "Content-Type: application/json" \
-d '{"original_url": "https://www.example.com"}'
curl -X POST http://localhost:3000/api/url \
-H "Content-Type: application/json" \
-d '{"original_url": "https://www.example.com", "custom_short_code": "mylink"}'
Just visit http://localhost:3000/abc123
(or whatever short code you got) and you'll be redirected to the original URL.
curl http://localhost:3000/api/url
curl http://localhost:3000/api/url/YOUR_URL_ID/stats
Want to modify the code? Here's what you need to know:
# Run all tests
npm test
# Run tests with coverage
npm run test:cov
# Run end-to-end tests
npm run test:e2e
# Check code style
npm run lint
# Format code
npm run format
src/
├── modules/
│ ├── app/ # Main app module and health checks
│ └── url/ # URL shortening logic
│ ├── controllers/ # API endpoints
│ ├── features/ # Business logic
│ ├── services/ # Database operations
│ ├── schemas/ # MongoDB models
│ └── dtos/ # Data validation
This service is production-ready for AWS deployment:
- DocumentDB Setup: Create an AWS DocumentDB cluster (MongoDB-compatible)
- Elastic Beanstalk: Deploy the app using the included
Procfile
- Environment Variables: Configure the following in EB:
MONGODB_URI
- Your DocumentDB connection stringPORT
- Will be set automatically by EBHOST
- Set to0.0.0.0
for container binding
The app automatically handles container deployment and scales with your traffic!
You can customize the app with these environment variables:
PORT
- Server port (default: 3000)MONGODB_URI
- MongoDB connection string (local) or DocumentDB connection string (AWS)HOST
- Server host (default: 0.0.0.0 for Docker/containers)
- NestJS - The main framework (like Express but fancier)
- MongoDB - Database for storing URLs
- Mongoose - MongoDB object modeling
- Jest - Testing framework
- Docker - For easy deployment
URL shorteners are everywhere, but sometimes you want your own. Maybe for privacy, custom domains, or just because you can! This project shows how to build a production-ready service with proper testing, clean architecture, and all the features you'd expect.