This is my solution to the URL Shortener Microservice challenge as part of freeCodeCamp's Back End Development and APIs Projects certification.
This URL shortener microservice allows users to shorten long URLs. Users can submit a URL to be shortened, and the service will generate a unique short URL. Additionally, users can access the original URL by visiting the short URL.
- POST a URL to
/api/shorturl
and receive a JSON response withoriginal_url
andshort_url
properties. - Visiting
/api/shorturl/<short_url>
redirects users to the original URL. - Handles invalid URLs and returns a JSON response with
{ error: 'invalid url' }
.
-
Submit a POST request to
/api/shorturl
with a JSON body containing the original URL. Example:{ "url": "https://www.example.com" }
-
Receive a JSON response with the original and short URL. Example:
{ "original_url": "https://www.example.com", "short_url": 1 }
-
Visit
/api/shorturl/1
to be redirected to the original URL.
-
Clone the repository:
git clone https://github.com/yourusername/url-shortener.git
-
Install dependencies:
npm install
-
Create a
.env
file and add your MongoDB connection URI:MONGO_URI=your_mongodb_uri
-
Start the server:
npm start
The server will be running on http://localhost:3000.