A progressive Node.js framework for building efficient and scalable server-side applications.
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
- Framework: NestJS (v11)
- Language: TypeScript
- Runtime: Node.js (LTS)
- Package Manager: pnpm
- Primary DB: MongoDB with Mongoose ODM
- Authentication: JWT with Passport.js
- Password Hashing: bcrypt
- Rate Limiting: @nestjs/throttler
- Security: Helmet, CORS, CSRF protection
- API Documentation: Swagger/OpenAPI
- Validation: Class Validator & Class Transformer
- Serialization: Class Transformer
- Request Validation: DTOs with decorators
- Testing: Jest (Unit, Integration, E2E)
- Code Quality: ESLint, Prettier, Husky
- CI/CD: GitHub Actions
- User logs in with credentials
- Server validates credentials and issues JWT access token and refresh token
- Access token is used for API authorization (short-lived)
- Refresh token is used to obtain new access tokens (long-lived)
- Admin: Full system access
- Manager: Manage users and content
- User: Basic access with limited permissions
- Guest: Read-only access (if applicable)
@Controller('protected')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('admin', 'manager')
export class ProtectedController {
// Controller methods
}-
Clone the repository
git clone https://github.com/yourusername/templateback.git cd yourproyect -
Install dependencies
pnpm install
-
Environment Setup
- Copy
.env.exampleto.env - Update the environment variables in
.envwith your configuration
- Copy
-
Database
- Ensure MongoDB is installed and running
- Update the database connection string in
.env
# 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# Build the application
$ pnpm run build
# Start in production mode
$ pnpm run start:prod# 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:watchOnce 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
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
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
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 deployWith Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure.
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.
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.
- Author - Kamil MyΕliwiec
- Website - https://nestjs.com
- Twitter - @nestframework
Nest is MIT licensed.