A simple and efficient URL shortener service built with Node.js, Express.js, and MongoDB that converts long URLs into short, easy-to-share links.
- Shorten long URLs to concise, easy-to-share links.
- Redirect users to the original URLs using shortened links.
- Track usage statistics for each shortened URL.
- Support for custom URL aliases (optional).
- Backend: Node.js, Express.js
- Database: MongoDB
- ORM: Mongoose
- Environment Management: dotenv
Ensure you have the following installed:
- Node.js (v14 or higher)
- MongoDB (local or cloud instance)
- Git (for cloning the repository)
-
Clone the repository:
git clone https://github.com/iampratt/URL_Shortener.git cd URL_Shortener
-
Install dependencies:
npm install
-
Set up environment variables:
- Create a
.env
file in the root directory. - Add the following variables:
PORT=<PORT> MONGO_URI=<your mongodb uri>
- Create a
-
Start the server:
npm run start
The server will run on
http://localhost:<PORT>
.
- Description: Shorten a long URL.
- Request Body:
{ "url": <Your URL> }
- Response:
{ "shortID": "http://localhost:3000/myalias" }
- Description: Redirect to the original long URL.
- Response: HTTP 200 redirect to the long URL if the code is valid.
- Error Response:
{ "err": "URL not found" }
- Description: Return analytics of shortID.
- Response: HTTP 200 redirect to the long URL if the code is valid.
- Error Response:
{ "err": "URL not found" }
- Description: Returns all registered URLs.
- Response: HTTP 200 redirect to the long URL if the code is valid.
URL_Shortener/
├── controllers/ # API logic (shortening and redirection)
├── models/ # Mongoose schemas for URLs
├── routes/ # API routes
├── .env # Environment variables
├── .gitignore # Files to ignore in Git
├── package.json # Project metadata and dependencies
└── server.js # Main server entry point
Contributions are welcome! Please open an issue or submit a pull request for improvements, bug fixes, or new features.
- GitHub: iampratt
- Email: [email protected]