A URL shortening service built with NestJS. This project allows users to shorten long URLs and retrieve the original URLs using the shortened ones.
- Shorten long URLs into compact, shareable links.
- Retrieve the original URL using the shortened link.
- Centralized logging for all API requests and responses.
- Environment-based configuration using
.env
files.
- Node.js (v16 or later)
- npm or yarn
- NestJS CLI (optional, for development)
-
Clone the repository:
git clone https://github.com/droiddevgeeks/TinyUrl.git cd TinyUrl
-
Install dependencies:
npm install
-
Create a .env file in the root directory and configure the following:
PORT=3000
-
Start the development server:
npm run start:dev
-
The application will be available at http://localhost:3000.
-
Shorten a URL POST /url/shorten
{ "originalUrl": "https://example.com" }
{ "shortUrl": "http://localhost:3000/abc123" }
-
Retrieve the Original URL GET /url/:shortUrl
{ "originalUrl": "https://example.com" }
src/
├── cache/
│ └──cache.module.ts # Redis cache module setup ([src/cache/cache.module.ts](src/cache/cache.module.ts))
│ └──cache.provider.ts # Redis client provider ([src/cache/cache.provider.ts](src/cache/cache.provider.ts))
│ └──cache.service.ts # Cache service abstraction ([src/cache/cache.service.ts](src/cache/cache.service.ts))
├── app.module.ts # Main application module
├── app.controller.ts # Root controller
├── app.service.ts # Root service
├── middleware/
│ └── logger.middleware.ts # Centralized logging middleware
| └── throttler.exception.filter.ts # Handles rate-limiting exceptions ([src/middleware/throttler.exception.filter.ts](src/middleware/throttler.exception.filter.ts))
│ └── throttler.gaurd.ts # Custom throttler guard with logging ([src/middleware/throttler.gaurd.ts](src/middleware/throttler.gaurd.ts))
├── url/
│ ├── tinyurl.controller.ts # URL controller
│ ├── tinyurl.service.ts # URL service
│ ├── dto/
│ │ └── create-url.dto.ts # Data Transfer Object for URL creation
All incoming requests and outgoing responses are logged using a custom middleware (LoggerMiddleware
). Logs include:
This project uses Redis for caching URL mappings to improve performance and reduce database load. The cache is managed via:
CacheModule
: Registers the cache service and provider.CacheProvider
: Configures and provides a Redis client.CacheService
: Exposes methods to set/get cache entries and manages Redis connection lifecycle.
Make sure to configure your .env
file with the correct Redis connection details.
throttler.exception.filter.ts
: Catches and formats rate-limiting errors with a user-friendly message.throttler.gaurd.ts
: Logs throttler keys for better monitoring and debugging of rate-limited requests.
This project is licensed under the MIT License.
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.