ShortURL is a URL shortening service built with Fastify, TypeScript, and MongoDB. It allows users to shorten long URLs and retrieve the original URLs using the shortened versions.
- Generate short URLs for long URLs.
- Redirect to the original URL using the short URL.
- MongoDB for persistent storage.
- Node.js (v16 or later)
- Docker (for MongoDB setup)
- TypeScript
-
Clone the repository:
git clone <repository-url> cd shorturl
-
Install dependencies:
npm install
-
Set up MongoDB:
- Ensure Docker is installed and running.
- Start MongoDB using Docker Compose:
docker-compose up -d
-
Configure environment variables:
- Update the
.envfile if necessary. The default values are:MONGO_URI=mongodb://root:example@localhost:27017 PORT=1234
- Update the
-
Run the development server:
npm run dev
-
Access the application:
- The server will be running at
http://localhost:1234/api.
- The server will be running at
- Description: Create a short URL for a given original URL.
- Request Body:
{ "originalUrl": "https://example.com" } - Response:
{ "_id": "unique-id", "originalUrl": "https://example.com", "shortUrl": "short-id" }
- Description: Redirect to the original URL using the short URL.
- Response:
- 302 Redirect to the original URL if found.
- 404 if the short URL does not exist.
shorturl/
├── src/
│ ├── common/
│ │ ├── models/ # Mongoose models
│ │ └── types/ # TypeScript type definitions
│ ├── core/ # Core application logic
│ ├── modules/ # Services and routes
│ └── index.ts # Application entry point
├── .env # Environment variables
├── docker-compose.yml # MongoDB setup
├── tsconfig.json # TypeScript configuration
└── package.json # Project metadata and scripts