LinkHash is a URL hashing system designed to shorten long URLs, preserve query parameters (such as UTM tracking), and ensure privacy-aware hashed URLs. The system supports features like single-use or limited-use URLs and click-tracking functionality for marketing purposes.
- URL Hashing: Generates shortened URLs by hashing long URLs with query parameters (e.g., UTM tracking).
- Click Tracking: Tracks the number of times each URL is clicked.
- Single-Use or Limited-Use URLs: The option to limit the number of uses per shortened URL.
- Query Parameter Preservation: Retains UTM and other query parameters when shortening and redirecting.
- Privacy Aware: Ensures that sensitive data is not exposed during URL hashing and redirection.
- Efficient & Scalable: Capable of handling large-scale URL shortening and tracking in high-traffic scenarios.
- Backend: Node.js, Express.js
- Database: MongoDB (for storing URLs, hash metadata, and click-tracking information)
- Hashing: Custom hashing logic for URL shortening
-
Clone the repository:
git clone https://github.com/yourusername/LinkHash.git cd LinkHash
-
Install dependencies:
npm install
-
Set up environment variables (see Environment Variables).
-
Run the server:
npm start
To start shortening URLs and tracking clicks, run the backend and use the API endpoints to generate and manage your shortened URLs.
You can use tools like Postman or cURL to test API endpoints. See API Endpoints for more details.
-
Shorten URL:
- Input:
https://www.example.com/?utm_source=google&utm_campaign=spring_sale
- Output:
https://linkhash.app/hash123
- Input:
-
Track Clicks: Each time
https://linkhash.app/hash123
is clicked, a count will be tracked in the database. -
Single or Limited Use: Set URLs to expire after a single or limited number of uses.
- POST
/api/shorten
- Description: Shortens a long URL, preserving query parameters.
- Request Body:
{ "original_url": "https://www.example.com/?utm_source=google&utm_campaign=spring_sale", "usage_limit": 10 // optional, default is unlimited }
- Response:
{ "shortened_url": "https://linkhash.app/hash123", "expires_at": null // or timestamp if limited }
- GET
/{hash}
- Description: Redirects the user to the original URL when the hashed URL is accessed.
- Response: HTTP 301 Redirect
- GET
/api/stats/{hash}
- Description: Retrieves the number of clicks for a specific shortened URL.
- Response:
{ "clicks": 42, "hash": "hash123", "original_url": "https://www.example.com" }
- POST
/api/invalidate/{hash}
- Description: Invalidates a single-use URL after it is clicked once.
Create a .env
file in the root directory and define the following variables:
PORT=3000
MONGO_URI=mongodb://localhost:27017/linkhash
BASE_URL=https://linkhash.app
HASH_SECRET=your_hash_secret_key
Run tests to ensure functionality:
npm test
You can implement unit and integration tests for the core functionalities like URL shortening, click tracking, and single-use URL invalidation.
Contributions are welcome! If you find bugs or want to add new features, feel free to fork the repository and create a pull request.