This is a simple URL shortening API built using Node.js, Express.js, and MongoDB. It allows users to submit long URLs for shortening and retrieve the original long URL when given the short URL.
Live Webserver: https://shawtify.up.railway.app/
- Shorten long URLs to unique, short keys.
- Redirect users to the original URL when accessing a shortened URL.
- Store URL mappings in a MongoDB database.
- Store counts of the number of times a URL has been visited.
Before you can run the URL Shortener application, make sure you have the following prerequisites installed:
- Node.js (v18)
- npm (Node Package Manager)
- MongoDB (Local or cloud-based instance)
-
Clone the repository to your local machine:
git clone https://github.com/Ahtritus/Shawtyfy.git cd url-shortener
-
Install the project dependencies:
npm install
- URL:
/shorten
- Method:
POST
- Request Body: JSON object with the
originalUrl
property. - Response: JSON object with the shortened URL.
- URL:
/:shortUrl
- Method:
GET
- Parameters:
shortUrl
- The short URL generated by the system. - Response: Redirects to the original URL associated with the short URL.
- URL:
/all
- Method:
GET
- Response: JSON array containing all currently saved URLs.
-
Shorten a URL:
POST /shorten Content-Type: application/json { "originalUrl": "https://example.com/my-long-url" }
Response:
{ "originalUrl": "https://example.com/my-long-url", "shortUrl": "abcdefgh" }
-
Retrieve the Original URL:
Accessing
https://localhost:3000/abcdefgh
will redirect you tohttps://example.com/my-long-url
. -
Get All URLs:
GET /all
Response:
[ { "originalUrl": "https://example.com/my-long-url", "shortUrl": "abcdefgh", "visited": 0 }, // Other URLs... ]
The URL Shortener application uses the following data model for storing URLs:
{
originalUrl: String, // The original long URL
shortUrl: String, // The shortened URL
visited: Number, // Number of times the URL has been visited
}
The application uses environment variables for configuration:
MONGO_CONNECTION
: The MongoDB connection string.
Make sure to set these environment variables before running the application.
To run tests for the application, execute the following command:
npm test
The URL Shortener application can be deployed to a server or cloud hosting platform of your choice. Ensure that you set the required environment variables and configure the MongoDB connection string for the production environment.
Contributions are welcome! If you find any issues or have suggestions for improvements, please create a GitHub issue or submit a pull request.
This project is licensed under the MIT License.