A simple and efficient URL shortener service built with Node.js, Express, and MongoDB. This project allows users to generate shortened URLs, track their usage, and analyze total clicks per URL.
- Shorten long URLs to a more manageable format
- Track and store the number of clicks for each shortened URL
- Retrieve analytics for each URL
- Clean and intuitive API for creating and managing shortened URLs
- Node.js and Express: Server-side JavaScript for handling API requests and responses.
- MongoDB: Database for storing URLs, their shortened versions, and analytics data such as click counts.
- Postman: Used for testing the API endpoints (GET, POST) to ensure all functionalities work as expected.
- Clone the repository:
git clone https://github.com/yourusername/url-shortener cd url-shortener
- Install dependencies():
npm install
- Start the server:
npm start
- Request: Send the original URL by pasting it in the form and submit it.
- Response: Returns a unique shortened URL ID at
http://localhost:8001/url
. - when we go back to home page ,i.e,
http://localhost:8001/
we will get a table with analytics data for previously shortened URLs, including the original URL, shortened URL ID, and total click count.
- Request: Access the shortened URL (e.g.,
http://localhost:8001/url/<shortId>
). - Response: Redirects to the original URL and updates the click count.
- This setup allows users to view URL shortening and analytics on a single page, making the user experience straightforward and consolidated.
- Go to
http://localhost:8001/
. - Enter a URL in the form and submit it.
- The new shortened URL will be created and saved to the database.
- Refresh the home page to see the updated analytics data for the new URL.
- Use
http://localhost:8001/url/<shortId>
to access the shortened URL, which will redirect to the original URL and update the click count.
├── controllers
│ └── url.js # Controller functions for handling requests
├── models
│ └── url.js # Mongoose schema for URL data (original URL, short ID, click count)
├── routes
│ └── url.js # URL routes for handling API requests
├── index.js # Main server file
└── README.md # Project documentation
Postman is a powerful tool for testing and interacting with the API endpoints. It allows you to send HTTP requests, view responses, and inspect headers to verify API functionality.
-
POST Request: Send a POST request to
http://localhost:8001/url
with a JSON payload containing the original URL. Postman will display the response, showing the shortened URL. -
GET Request: Access
http://localhost:8001/<shortId>
in Postman or your browser. This will redirect to the original URL and increment the click count. -
Analytics Request: Send a GET request to
http://localhost:8001/analytics/<shortId>
to retrieve analytics data, such as the total clicks for a shortened URL.
- Add user authentication to manage URLs.
- Implement expiration dates for shortened URLs.
- Include additional analytics, such as tracking locations or timestamps for each click.
Feel free to contribute or report any issues. Happy coding!