A simple and efficient URL shortener service built with Node.js, Express, and MongoDB. Generate short links that redirect to original URLs โ perfect for personal projects, frontend integration, or learning backend development.
- Generate unique short URLs
- Redirect to original URLs via short codes
- Simple and clean REST API
- Click tracking
- Easy MongoDB setup via
.env - Modular and scalable project structure
- Node.js + Express โ Fast and flexible backend framework
- MongoDB + Mongoose โ Schema-based database modeling
- nanoid โ For generating unique short codes
git clone https://github.com/Prerit008/url-shortner.git
cd url-shortener
npm install
Create a .env file in the root directory with the following variables:
PORT=5000
MONGO_URI=mongodb://localhost:27017/urlshortener
BASE_URL=http://localhost:5000
Replace MONGO_URI with your MongoDB Atlas URI if you're deploying online.
npm run dev # For development with nodemon
npm start # For production
โ POST /shorten
Request Body:
{"originalUrl": "https://www.google.com"}
Response:
{"shortUrl": "http://localhost:5000/abc123X"}
Redirect to the original URL using the short code.
Visit http://localhost:5000/abc123X in your browser and it will redirect you to the original URL.
A simple and elegant frontend is included with the project using HTML + Tailwind CSS. It provides a minimal UI to interact with the backend and shorten URLs.
- Open the index.html file in your browser.
- Enter any valid long URL
e.g., https://www.google.com. - Click the "Shorten URL" button.
- The shortened URL will appear below the input and link to the original.
- Make sure your backend
http://localhost:5000is running before using the frontend.
You can customize this page, integrate it into a bigger app, or deploy it as a standalone client on Vercel, Netlify, or similar platforms.
Contributions are welcome! Feel free to open issues, suggest new features, or submit pull requests to improve this project.