π URL Shortener Project
A lightweight, efficient URL Shortener application built with Node.js, Express, and MongoDB. This backend-focused project allows users to convert long URLs into short ones and redirect back to the original destinationβshowcasing key backend skills such as API development, error handling, and database management.
β¨ Features
- π Shorten URLs: Generate unique, shortened links
- βͺοΈ Redirection: Automatically redirect short URLs to the original link
- π‘ REST API: Clean endpoints for interacting with the application
- πΎ MongoDB Integration: URL mappings stored using Mongoose ODM
- π« Error Handling: Invalid URLs and non-existent short codes handled gracefully
- π§ Modular Design: Easily extendable for auth, analytics, and more
π Tech Stack
- Backend: Node.js, Express.js
- Database: MongoDB (with Mongoose)
- Utilities: dotenv, nanoid or shortid for unique code generation
π Folder Structure
Url-Shortner/
βββ controllers/
βββ models/
βββ routes/
βββ .env
βββ app.js
βββ package.json
βββ README.md
βοΈ How It Works
- A POST request is made with a long URL
- Server generates a short ID and stores the mapping in MongoDB
- A GET request with the short code redirects to the original URL
π Sample API Usage
β Create Short URL
POST /api/shorten
{
"originalUrl": "https://www.example.com"
}
Response:
{
"shortUrl": "https://yourdomain.com/abc123"
}
π Redirect
GET /:shortCode
Redirects user to the original long URL
π§ͺ Running Locally
git clone https://github.com/Sonupandit9693/Learning-Node-js-Project.git
cd Learning-Node-js-Project/Url-Shortner
npm install
-
Create a
.env
file with:MONGODB_URI=your-mongodb-connection-string BASE_URL=http://localhost:3000
npm start
- Test the API using Postman, Thunder Client, or a browser.
π What I Learned
- REST API architecture with Express
- MongoDB schema modeling and integration
- Error handling and async programming
- Environment configuration and scalable folder structures