A URL shortening service built with NestJS framework and PostgreSQL database using Prisma ORM.
This project provides a simple and efficient URL shortening service that allows users to convert long URLs into shorter, more manageable links. It includes features like custom short codes, click tracking, and URL management.
- ✨ Shorten long URLs with auto-generated or custom short codes
- 📊 Click tracking and analytics
- 🔗 Redirect to original URLs
- 📋 List all shortened URLs
- 🗑️ Delete URLs
- 🔧 RESTful API endpoints
- 🛡️ Input validation with class-validator
- 🗄️ PostgreSQL database with Prisma ORM
- 🌐 CORS enabled
- Framework: NestJS
- Database: PostgreSQL
- ORM: Prisma
- Validation: class-validator, class-transformer
- Testing: Jest
$ npm install
- Set up your PostgreSQL database
- Copy the environment variables:
cp .env.example .env
-
Update the
DATABASE_URL
in .env file -
Run database migrations:
npx prisma migrate dev
- Generate Prisma client:
npx prisma generate
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
Create a shortened URL
Request Body:
{
"originalUrl": "https://example.com/very/long/url",
"customCode": "mycode" // optional
}
Response:
{
"id": 1,
"shortCode": "abc123",
"originalUrl": "https://example.com/very/long/url",
"clickCount": 0,
"createdAt": "2024-01-01T00:00:00.000Z",
"shortUrl": "http://localhost:3000/abc123"
}
Redirect to the original URL and increment click counter
Get all shortened URLs
Response:
[
{
"id": 1,
"shortCode": "abc123",
"originalUrl": "https://example.com/very/long/url",
"clickCount": 5,
"createdAt": "2024-01-01T00:00:00.000Z",
"updatedAt": "2024-01-01T00:00:00.000Z"
}
]
Get statistics for a specific short URL
Response:
{
"id": 1,
"shortCode": "abc123",
"originalUrl": "https://example.com/very/long/url",
"clickCount": 5,
"createdAt": "2024-01-01T00:00:00.000Z",
"shortUrl": "http://localhost:3000/abc123"
}
Delete a shortened URL
Response:
{
"message": "URL deleted successfully"
}
src/
├── app.controller.ts # Main app controller
├── app.module.ts # Main app module
├── app.service.ts # Main app service
├── main.ts # Application entry point
├── prisma/ # Prisma configuration
│ ├── prisma.module.ts # Prisma module
│ └── prisma.service.ts # Prisma service
└── urls/ # URLs feature module
├── dto/ # Data transfer objects
├── entities/ # Entity definitions
├── urls.controller.ts # URLs controller
├── urls.module.ts # URLs module
└── urls.service.ts # URLs service
- UrlsController: Handles HTTP requests for URL operations
- UrlsService: Contains business logic for URL management
- PrismaService: Database connection and operations
- CreateUrlDto: Validation for URL creation
- UrlResponseDto: Response format for URL data
DATABASE_URL
: PostgreSQL connection stringBASE_URL
: Base URL for the application (default: http://localhost:3000)PORT
: Port number (default: 3000)TZ
: Timezone (default: Asia/Bangkok)
The application uses a single Url
model defined in prisma/schema.prisma:
id
: Auto-incrementing primary keyshortCode
: Unique short code for the URLoriginalUrl
: The original long URLclickCount
: Number of times the URL has been accessedcreatedAt
: Timestamp when the URL was createdupdatedAt
: Timestamp when the URL was last updated
This project is MIT licensed.