This is a URL shortener application that allows users to create short links for long URLs and retrieve the original URLs using the short links. The application uses a MySQL database for persistent storage and Redis for caching the URLs for faster access.
- Create a short URL from a long URL.
- Retrieve the original URL by using the short URL.
- Cache URLs in Redis for faster retrieval.
- Handle URL validation before creating short URLs.
- Node.js
- Express.js
- MySQL
- Redis
- TypeScript
NOTE: Redis and MySQL need to be installed first. Refer to the official Redis documentation for installation instructions.
-
Clone the repository:
git clone https://github.com/teispace/tei_url_shortner.git
-
Navigate to the project directory:
cd url-shortener -
Install dependencies:
npm install
-
Create a
.envfile in the root directory and set the following environment variables:APP_URL=http://localhost:8080 DB_HOST=your_db_host DB_USER=your_db_user DB_PASSWORD=your_db_password DB_NAME=your_db_name DB_PORT=your_db_portOr you can use the provided
.env.examplefile as a template:cp .env.example .env
Update the
.envfile with your database credentials. -
Start the MySQL server and Redis server.
-
Run the application:
npm start
-
To create a short URL, send a POST request to
/create-urlwith a JSON body containing theurlparameter:{ "url": "https://example.com" } -
To retrieve the original URL, make a GET request to
/{shortUrl}where{shortUrl}is the short URL code.
src/models/url.model.ts: Contains theUrlModelclass for interacting with the MySQL database, handling the creation and retrieval of URLs.src/controllers/url.controller.ts: Contains theUrlControllerclass that manages the HTTP requests and responses, validating input, and coordinating between the model and view.src/config/db.config.ts: Handles the database connection and configuration, including methods for querying and creating the URL table.src/config/redis.config.ts: Configures the Redis client for caching short URLs to improve performance.src/utils/urlUtils.ts: Contains utility functions for URL validation and generating short URLs.
Contributions are welcome! Please feel free to submit a pull request or open an issue for any improvements or bug fixes.
This project is licensed under the MIT License - see the LICENSE file for details.