An open API service indexing awesome lists of open source software.

https://github.com/codegetters/nestjs-template

nestjs start template
https://github.com/codegetters/nestjs-template

Last synced: about 2 months ago
JSON representation

nestjs start template

Awesome Lists containing this project

README

          

# NestJS Template

[δΈ­ζ–‡](README.md) | [English](README-EN.md)

A full-featured NestJS backend project template with common functionalities and best practices.

## Features

- πŸš€ High-performance HTTP server based on Fastify
- πŸ“ Integrated Swagger documentation (OpenAPI)
- πŸ”’ Global exception handling and unified response format
- πŸ“Š Winston logging system (with daily rotation)
- πŸ’Ύ Prisma ORM integration
- ⚑️ Request timeout handling
- ✨ Unified response transformation
- 🦒 support vercel one-click deployment

## Project Structure

```
src/
β”œβ”€β”€ api/ # API modules directory
β”‚ β”œβ”€β”€ demo/ # Example module
β”‚ └── index.ts # API route registration
β”œβ”€β”€ common/ # Common modules
β”‚ β”œβ”€β”€ logger/ # Logging module
β”‚ └── prisma/ # Prisma database module
β”œβ”€β”€ core/ # Core functionality
β”‚ β”œβ”€β”€ decorator/ # Custom decorators
β”‚ β”œβ”€β”€ filter/ # Exception filters
β”‚ β”œβ”€β”€ guard/ # Guards
β”‚ └── interceptor/ # Interceptors
└── main.ts # Application entry point
```

## Quick Start

### Requirements

- Node.js >= 16
- pnpm >= 8

### Install Dependencies

```bash
pnpm install
```

### Configure Environment Variables

```bash
cp .env.example .env
```

### Database Connection

Uncomment the code at `src/common/prisma/prisma.service.ts:51` and set up the database connection

### Start the Project

```bash
# dev
pnpm run start:dev

# prod
pnpm run start:prod

# build
pnpm run build
```

### Access Documentation

```bash
http://localhost:8080/docs
```

## One-Click Deploy

> When Vercel is deployed, an empty LoggerService will be introduced, because Vercel has its own log system, and using the original log system will cause the operation to fail.

Deploy the template using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=vercel-examples):

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/CodeGetters/nestjs-template)

## Main Features

### Unified Response Format

Success response format

```json
{
"code": "SUCCESS",
"data": {},
"message": "Request successful",
"timestamp": "2024-01-01T00:00:00.000Z"
}
```

Error response format

```json
{
"code": "ERROR_CODE",
"message": "Error message",
"timestamp": "2024-01-01T00:00:00.000Z",
"path": "/api/xxx"
}
```

### Logging System

- Automatic daily log file rotation
- Separate error and combined logs
- Console output in development environment
- Log files stored in logs directory

### Exception Handling

- Global exception filter for all uncaught exceptions
- HTTP exception filter for standard HTTP exceptions
- Unified handling of Prisma database exceptions

### Request Timeout Handling

Default 60-second timeout limit, configurable via TimeoutInterceptor

## Development Guide

### Adding New Modules

1. Create a new module directory in `src/api`, e.g., `user`
2. Define service in `src/api/user/user.service.ts`
3. Define controller in `src/api/user/user.controller.ts`
4. Define module in `src/api/user/user.module.ts`
5. Register new module in `src/api/index.ts`

### Using Custom Response Format

Use `@SkipTransform()` decorator to skip unified response transformation:

```typescript
@Get()
@SkipTransform()
getData() {
return { raw: 'data' };
}
```

## Contributing

Issues and Pull Requests are welcome

## License

MIT