A simple, fast, and efficient backend API for creating and managing short URLs. This service takes a long URL and generates a unique, short identifier that redirects to the original URL, perfect for sharing cleaner links on social media, in emails, or in any situation where space is limited.
- Generate Short URLs: Provide a long URL and get a unique short ID in return.
- URL Redirection: Accessing the short URL path (e.g., http://yourdomain.com/shortId) will redirect the user to the original long URL.
- Runtime: Node.js
- Framework: Express.js
- Database: MongoDB with Mongoose ODM
- Short ID Generation: nanoid for creating unique, URL-friendly IDs.
- Environment Variables: dotenv
Follow these steps to get the project running on your local machine.
-
Clone the repository:
git clone [https://github.com/shubham019-ai/link-shortener-api.git\](https://github.com/shubham019-ai/link-shortener-api.git)
cd link-shortener-api -
Install the dependencies:
npm install -
Set up Environment Variables:
Create a file named .env in the root of the project and add the following variables. Replace the placeholder value with your actual MongoDB connection string.
PORT=8001
MONGO_URL=your_mongodb_connection_string -
Start the server:
To run the server in development mode with automatic restarts:
npm run devTo run the server in production:
npm startThe server will start on the port you defined in your .env file (e.g., http://localhost:8001).
Here is a summary of the available API routes.
-
Description: Creates a new short URL.
-
Request Body (JSON):
{
"url": "[https://www.example-long-url.com/some/very/long/path\](https://www.example-long-url.com/some/very/long/path)"
} -
Successful Response (200 OK):
{
"id": "Jk8sL9pQ"
}
- Description: Redirects the user to the original long URL associated with the provided shortId. This endpoint also records the visit.
- Example Request: GET http://localhost:8001/Jk8sL9pQ
- Successful Response: A 302 Found redirect to the original URL.
- Shubham Maurya
- GitHub: @shubham019-ai