This project provides a URL shortening service backend using Node.js, Express.js, and MongoDB.
- Shorten a long URL to a unique short URL.
- Redirect to the original long URL using the short URL.
- Handle errors gracefully.
- RESTful API endpoints for URL shortening.
Before starting, ensure you have the following installed:
- Node.js (v14.x or higher)
- MongoDB
- npm or yarn
-
Clone the repository:
git clone https://github.com/mdrianislam0or1/generates-short-urls-backend.git cd generates-short-urls-backend Install dependencies: bash npm install ## or yarn install
Set up environment variables:
Create a .env file in the root directory based on .env.example and configure your MongoDB connection URL.
Setting Up
Create a .env file in the root directory based on .env.example and configure your MongoDB connection URL:
plaintext
```PORT=5000
DATABASE_URL=mongodb+srv://your-username:your-password@your-cluster.mongodb.net/your-database?retryWrites=true&w=majority
NODE_ENV=development
```
Start the server:
bash
npm start
## or
yarn start
This will start the server at http://localhost:5000.
POST /api/url
Endpoint to shorten a long URL.
Request body:
json
{ "longUrl": "https://example.com/very-long-url" }
json
{ "success": true, "message": "Short URL generated successfully", "data": "short-url-code" }
GET /api/url/
Redirect to the original long URL using the short URL.
Example:
vbnet
GET /api/url/short-url-code
Response:
vbnet
302 Found
Location: https://example.com/very-long-url
Error Handling
Invalid or missing request parameters.
Duplicate URL entries.
Not found errors for unknown short URLs.
Contributions are welcome! Please fork the repository and create a pull request with your improvements.