This project is a simple URL shortener service built using Node.js, Express, and MongoDB. It allows users to shorten long URLs and track visit history for each shortened URL.
- Database: MongoDB is used to store the original URL, the shortened URL identifier (
shortId), and visit history (timestamps of visits). - Backend: Express.js is used to handle API requests and responses.
- Short ID Generation: The
nanoidlibrary is used to generate unique short IDs for URLs. - Redirection: When a user accesses a shortened URL, the service redirects them to the original URL and logs the visit timestamp.
-
Clone the repository:
git clone <repository-url> cd URL_Shortener
-
Install dependencies:
npm install
-
Set up MongoDB:
- Ensure you have a MongoDB instance running.
- Update the MongoDB connection string in the configuration file (e.g.,
connect.js).
-
Start the server:
npm start
-
The server will run on
http://localhost:8002.
Request:
POST /url
Content-Type: application/json
{
"url": "https://example.com"
}Response:
{
"shortId": "abc123",
"redirectURL": "https://example.com",
"visitHistory": []
}Request:
GET /url/abc123Response:
Redirects to https://example.com.
Request:
GET /url/abc123/historyResponse:
{
"shortId": "abc123",
"redirectURL": "https://example.com",
"visitHistory": [
{ "timestamp": 1698765432100 },
{ "timestamp": 1698765432200 }
]
}- Ensure your MongoDB connection string is valid and accessible.
- The project uses
nanoidfor generating unique short IDs.