Skip to content

Repository files navigation

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Discord Backers on Open Collective Sponsors on Open Collective Donate us Support us Follow us on Twitter

Description

This project is a robust backend template built with NestJS and MongoDB, designed to accelerate API development with production-ready features. It implements a secure, scalable architecture with comprehensive authentication and role-based access control (RBAC) out of the box.

Key features include:

  • πŸ” JWT-based authentication with refresh tokens
  • πŸ‘₯ Role-based access control (RBAC)
  • πŸ›‘οΈ Security best practices (Helmet, CORS, Rate Limiting)
  • πŸ“ API documentation with Swagger
  • πŸ§ͺ Comprehensive test coverage
  • πŸ”„ Database migrations and seeders
  • πŸš€ Docker support for development and production

⚠️ Security Note: While this template implements security best practices, always conduct a thorough security review before deploying to production. If you discover any vulnerabilities, please report them by following the instructions in SECURITY.md

πŸš€ Tech Stack

Core

  • Framework: NestJS (v11)
  • Language: TypeScript
  • Runtime: Node.js (LTS)
  • Package Manager: pnpm

Database

  • Primary DB: MongoDB with Mongoose ODM

Authentication & Authorization

  • Authentication: JWT with Passport.js
  • Password Hashing: bcrypt
  • Rate Limiting: @nestjs/throttler
  • Security: Helmet, CORS, CSRF protection

API & Documentation

  • API Documentation: Swagger/OpenAPI
  • Validation: Class Validator & Class Transformer
  • Serialization: Class Transformer
  • Request Validation: DTOs with decorators

Development Tools

  • Testing: Jest (Unit, Integration, E2E)
  • Code Quality: ESLint, Prettier, Husky
  • CI/CD: GitHub Actions

πŸ” Authentication & Authorization

Authentication Flow

  1. User logs in with credentials
  2. Server validates credentials and issues JWT access token and refresh token
  3. Access token is used for API authorization (short-lived)
  4. Refresh token is used to obtain new access tokens (long-lived)

Role-Based Access Control (RBAC)

  • Admin: Full system access
  • Manager: Manage users and content
  • User: Basic access with limited permissions
  • Guest: Read-only access (if applicable)

Protected Routes

@Controller('protected')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('admin', 'manager')
export class ProtectedController {
  // Controller methods
}

πŸ› οΈ Project Setup

  1. Clone the repository

    git clone https://github.com/yourusername/templateback.git
    cd yourproyect
  2. Install dependencies

    pnpm install
  3. Environment Setup

    • Copy .env.example to .env
    • Update the environment variables in .env with your configuration
  4. Database

    • Ensure MongoDB is installed and running
    • Update the database connection string in .env

πŸš€ Running the Application

Development

# Start in development mode with hot-reload
$ pnpm run start:dev

# Access the application at http://localhost:3000
# API documentation available at http://localhost:3000/api/docs

Production

# Build the application
$ pnpm run build

# Start in production mode
$ pnpm run start:prod

πŸ§ͺ Testing

# Run unit tests
$ pnpm run test

# Run e2e tests
$ pnpm run test:e2e

# Generate test coverage report
$ pnpm run test:cov

# Run tests in watch mode
$ pnpm run test:watch

πŸ“š API Documentation

Once the application is running, you can access the interactive API documentation at:

  • Swagger UI: http://localhost:3000/api/docs
  • JSON format: http://localhost:3000/api/docs-json

πŸ“¦ Project Structure

src/
β”œβ”€β”€ auth/                   # Authentication module
β”‚   β”œβ”€β”€ decorators/         # Custom decorators
β”‚   β”œβ”€β”€ dto/               # Data Transfer Objects
β”‚   β”œβ”€β”€ guards/            # Authentication guards
β”‚   β”œβ”€β”€ tests/             # Test files
β”‚   └── *.ts               # Core auth files
β”‚
β”œβ”€β”€ chat/                   # Chat module
β”‚   β”œβ”€β”€ controllers/       # Request handlers
β”‚   β”œβ”€β”€ dto/              # Data Transfer Objects
β”‚   β”œβ”€β”€ gateways/         # WebSocket gateways
β”‚   β”œβ”€β”€ schemas/          # Database schemas
β”‚   β”œβ”€β”€ services/         # Business logic
β”‚   └── tests/            # Test files
β”‚
β”œβ”€β”€ common/                # Shared utilities
β”‚   β”œβ”€β”€ middleware/       # Global middleware
β”‚   β”œβ”€β”€ schemas/          # Common schemas
β”‚   β”œβ”€β”€ services/         # Shared services
β”‚   └── types/            # TypeScript types
β”‚
β”œβ”€β”€ config/                # Application configuration
β”‚   β”œβ”€β”€ *.config.ts       # Configuration files
β”‚   └── database/         # Database configuration
β”‚
β”œβ”€β”€ event-failure/         # Event failure handling
β”‚   β”œβ”€β”€ controllers/      # Request handlers
β”‚   β”œβ”€β”€ dto/             # Data Transfer Objects
β”‚   β”œβ”€β”€ schemas/         # Database schemas
β”‚   └── services/        # Business logic
β”‚
β”œβ”€β”€ role-manager/          # Role management
β”‚   β”œβ”€β”€ controllers/      # Request handlers
β”‚   β”œβ”€β”€ dto/             # Data Transfer Objects
β”‚   β”œβ”€β”€ enums/           # Enumerations
β”‚   β”œβ”€β”€ schemas/         # Database schemas
β”‚   β”œβ”€β”€ services/        # Business logic
β”‚   └── tests/           # Test files
β”‚
β”œβ”€β”€ user/                  # User management
β”‚   β”œβ”€β”€ controllers/      # Request handlers
β”‚   β”œβ”€β”€ dto/             # Data Transfer Objects
β”‚   β”œβ”€β”€ repositorys/     # Data access layer
β”‚   β”œβ”€β”€ schemas/         # Database schemas
β”‚   β”œβ”€β”€ services/        # Business logic
β”‚   └── tests/           # Test files
β”‚
β”œβ”€β”€ app.module.ts         # Root module
└── main.ts              # Application entry point

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Deployment

When you're ready to deploy your NestJS application to production, there are some key steps you can take to ensure it runs as efficiently as possible. Check out the deployment documentation for more information.

If you are looking for a cloud-based platform to deploy your NestJS application, check out Mau, our official platform for deploying NestJS applications on AWS. Mau makes deployment straightforward and fast, requiring just a few simple steps:

$ pnpm install -g @nestjs/mau
$ mau deploy

With Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure.

Resources

Check out a few resources that may come in handy when working with NestJS:

  • Visit the NestJS Documentation to learn more about the framework.
  • For questions and support, please visit our Discord channel.
  • To dive deeper and get more hands-on experience, check out our official video courses.
  • Deploy your application to AWS with the help of NestJS Mau in just a few clicks.
  • Visualize your application graph and interact with the NestJS application in real-time using NestJS Devtools.
  • Need help with your project (part-time to full-time)? Check out our official enterprise support.
  • To stay in the loop and get updates, follow us on X and LinkedIn.
  • Looking for a job, or have a job to offer? Check out our official Jobs board.

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

License

Nest is MIT licensed.

About

Practical template for building APIs with Typescript & mongoDB

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Sponsor this project

Contributors

Languages