A full-stack URL shortener application with a Python Flask backend and React frontend.
π Live Demo: https://url-shortener.sadlers.cloud/
- β Create short URLs from long URLs
- β Redirect short URLs to original URLs
- β Multiple short URLs allowed for same long URL (for tracking)
- β Base62 encoding for short, readable URLs
- β SQLite database for persistence
- β IDs start at 10000 for better-looking short URLs
- β CORS support for frontend integration
- β Comprehensive error handling and validation
- β Modern, responsive UI with gradient background
- β Real-time URL validation
- β Copy shortened URL to clipboard
- β Test shortened URLs directly in the interface
- β Loading states and error handling
- β Success notifications
./start-dev.shThis will start both the backend (port 8000) and frontend (port 3000) automatically.
-
Set up Virtual Environment
python3 -m venv venv source venv/bin/activate -
Install Python dependencies:
pip install -r requirements.txt
-
Start the backend:
python main.py
-
Install Node.js dependencies:
cd frontend npm install -
Start the React development server:
npm start
- Frontend: http://localhost:3000
- Backend API: http://localhost:8000
Test if the server is running:
curl http://localhost:8000/Expected Response:
{ "message": "URL Shortener API is running!" }Convert a long URL to a short one:
curl -X POST \
-H "Content-Type: application/json" \
-d '{"url": "https://www.google.com"}' \
http://localhost:8000/shortenExpected Response:
{ "short_url": "2Bi" }Note: Your actual short URL will vary but will be 3+ characters
Use the short URL to redirect to the original:
curl -L http://localhost:8000/2BiThis will redirect you to https://www.google.com (showing in the CLI the HTML content from Google's homepage)
alternatively:
curl -I http://localhost:8000/2BiThis will show you the redirect response instead of following it (cleaner in the CLI)
Create several short URLs:
# GitHub
curl -X POST \
-H "Content-Type: application/json" \
-d '{"url": "https://github.com/EmiSadler/URL_shortener"}' \
http://localhost:8000/shorten
# Bootcamp Simulator
curl -X POST \
-H "Content-Type: application/json" \
-d '{"url": "https://bootcampsim.netlify.app/"}' \
http://localhost:8000/shortenTest with missing URL:
curl -X POST \
-H "Content-Type: application/json" \
-d '{}' \
http://localhost:8000/shortenTest with non-existent short URL:
curl http://localhost:8000/xyz123./build-prod.shOr manually:
cd frontend
npm run buildOption 1 - Using a static file server:
npm install -g serve
serve -s frontend/build -l 3000Option 2 - Using Python's built-in server:
cd frontend/build
python -m http.server 3000For production deployment:
-
Backend: Use a WSGI server like Gunicorn
pip install gunicorn gunicorn -w 4 -b 0.0.0.0:8000 main:create_app() -
Frontend: Serve the built files using a web server like Nginx or Apache
-
Database: Consider using PostgreSQL for production instead of SQLite
| Method | Endpoint | Description |
|---|---|---|
GET |
/ |
Health check |
POST |
/shorten |
Create short URL from long URL |
GET |
/<short_url> |
Redirect to original URL |
URL_shortener/
βββ app/ # Backend Flask application
β βββ __init__.py
β βββ config.py # Configuration settings
β βββ db.py # Database connection and initialization
β βββ models.py # URL model and database operations
β βββ routes.py # Flask API routes
β βββ shortener.py # Short URL generation logic (base62)
β βββ validators.py # Input validation functions
β βββ error_handlers.py # Error handling utilities
βββ frontend/ # React frontend application
β βββ public/
β β βββ index.html # HTML template
β β βββ manifest.json # PWA manifest
β βββ src/
β β βββ components/ # React components
β β β βββ Header.js # App header
β β β βββ Footer.js # App footer
β β β βββ URLShortener.js # Main URL shortener form
β β βββ App.js # Main App component
β β βββ index.js # React entry point
β β βββ index.css # Global styles
β βββ package.json # Node.js dependencies
β βββ build/ # Production build (after npm run build)
βββ tests/ # Test files for backend
βββ .github/ # GitHub Actions CI/CD
βββ docs/ # Documentation
βββ scripts/ # Utility scripts
βββ main.py # Backend entry point
βββ requirements.txt # Python dependencies
βββ start-dev.sh # Development startup script
βββ build-prod.sh # Production build script
βββ url_shortener.db # SQLite database (created automatically)
βββ README.md
- URL Submission: User submits a long URL via POST request
- ID Generation: Database assigns an ID starting from 10000
- Short Code Creation: ID is converted to base62 (0-9, a-z, A-Z)
- Database Storage: Both original URL and short code are saved
- Response: Short code is returned to user
- Database: SQLite with auto-incrementing IDs starting at 10000
- Encoding: Base62 encoding for compact, readable URLs
- Port: Runs on port 8000
- Framework: Flask
- URL Format:
http://localhost:8000/<short_code>
Port 8000 in use?
- Kill the process:
lsof -ti:8000 | xargs kill -9 - Or change the port in
main.py
Database issues?
- Delete and recreate:
rm url_shortener.dbthen restart the app