A simple URL shortener application built using Node.js, Express, and MongoDB.
This URL shortener application allows you to create short and easy-to-share versions of long URLs. It uses Node.js and Express for the backend server and MongoDB as the database to store URL mappings.
- Shorten long URLs into easy-to-share short URLs.
- Record and display the total number of clicks for each shortened URL.
- Track the timestamp of each click.
- Store URL mappings in a MongoDB database.
- Validate URLs to ensure they are in the correct format.
Before you begin, ensure you have met the following requirements:
- Node.js and npm installed on your machine.
- MongoDB installed and running locally or on a remote server.
- Git (optional, for cloning the repository).
-
Clone the repository (if you haven't already):
git clone <repository_url> cd url-shortener-app
-
Install the application dependencies:
npm install
-
Configure your MongoDB connection in the .env file:
Create a .env file in the project root directory and add the following:
PORT=4000 MONGODB_URI=your_mongodb_connection_string BASE_URL=http://localhost:3000 # Update with your base URL
-
Start the application:
npm start
-
Access the application in your web browser at http://localhost:3000 (or your specified base URL).
-
Shorten a URL by entering it in the provided form and clicking "Shorten."
-
Click on the shortened URL to redirect to the original URL.
-
Endpoint: /api/shorten
-
Method: POST
-
Request Body: JSON object with origUrl
Example:
curl -X POST http://localhost:4000/api/shorten -H "Content-Type: application/json" -d "{\"originalUrl\": \"https://google.com/\"}"
- Endpoint: /:urlId
- Method: GET
- Endpoint: /stats/:urlId
- Method: GET