A production-ready NestJS application boilerplate with Behavior-Driven Development (BDD) testing, Clean Architecture principles, and comprehensive authentication system.
- Clean Architecture: Well-structured codebase following Domain-Driven Design principles
- BDD Testing: Cucumber.js integration for behavior-driven development
- Authentication: JWT and Google OAuth2 authentication strategies
- Database: Prisma ORM with PostgreSQL
- Email Service: Nodemailer integration with template support
- API Documentation: Swagger/OpenAPI integration
- Validation: Class-validator and class-transformer for DTOs
- Security: Passport.js strategies, bcrypt password hashing
- Code Quality: ESLint, Prettier, Husky, and Commitlint
- Type Safety: Full TypeScript support
src/
ββ application/ # Application services / use-cases
β ββ auth/ # Authentication business logic
β ββ user/ # User management business logic
ββ common/ # Shared utilities, decorators, constants, DTOs
β ββ constants/ # Application constants
β ββ decorators/ # Custom decorators
β ββ dto/ # Shared DTOs
β ββ service/ # Common services
β ββ types/ # Type definitions
β ββ utils/ # Utility functions
ββ core/ # Core logic (exceptions, interceptors, guards)
β ββ exceptions/ # Custom exceptions
β ββ filters/ # Exception filters
β ββ interceptors/ # Response interceptors
β ββ middleware/ # Custom middleware
β ββ pipes/ # Validation pipes
ββ domain/user/ # Domain entities and business logic
β ββ user.entity.ts
β ββ user.repository.interface.ts
β ββ user.repository.ts
ββ infrastructure/ # Database, API integrations, repositories
β ββ auth/ # Authentication strategies and guards
β ββ config/ # Configuration modules
β ββ database/ # Database configuration
β ββ mail/ # Email service
β ββ storage/ # File storage service
β ββ third-party/ # External API integrations
ββ presentation/ # Controllers, GraphQL resolvers, API endpoints
β ββ controllers/ # REST API controllers
ββ app.module.ts # Root module
ββ main.ts # Entry point (bootstrap NestJS)
- Framework: NestJS
- Language: TypeScript
- Database: PostgreSQL with Prisma ORM
- Authentication: JWT + Google OAuth2
- Testing: Jest + Cucumber.js
- Email: Nodemailer
- Documentation: Swagger/OpenAPI
- Validation: Class-validator
- Security: Passport.js, bcrypt
- Node.js (v18 or higher)
- pnpm (recommended) or npm
- PostgreSQL database
- Google OAuth2 credentials (for Google authentication)
git clone <repository-url>
cd nest_bdd_boilerplatepnpm installCreate a .env file in the root directory:
# Database
DATABASE_URL="postgresql://username:password@localhost:5432/database_name"
# JWT
JWT_SECRET="your-jwt-secret-key"
JWT_EXPIRES_IN="7d"
# Google OAuth2
GOOGLE_CLIENT_ID="your-google-client-id"
GOOGLE_CLIENT_SECRET="your-google-client-secret"
GOOGLE_CALLBACK_URL="http://localhost:3000/auth/google/callback"
# Email Configuration
MAIL_HOST="smtp.gmail.com"
MAIL_PORT=587
MAIL_USER="your-email@gmail.com"
MAIL_PASS="your-app-password"
MAIL_FROM="your-email@gmail.com"
# Application
PORT=3000
NODE_ENV="development"# Generate Prisma client
pnpm prisma generate
# Run database migrations
pnpm prisma migrate dev
# (Optional) Seed the database
pnpm prisma db seed# Development mode
pnpm start:dev
# Production mode
pnpm build
pnpm start:prodThe application will be available at http://localhost:3000
# Run unit tests
pnpm test
# Run tests in watch mode
pnpm test:watch
# Run tests with coverage
pnpm test:cov# Run BDD tests
pnpm test:e2etests/
ββ features/ # Gherkin feature files
β ββ user/
β ββ suspend.feature
ββ steps/ # Step definitions
ββ user/
ββ user.step.ts
Once the application is running, you can access the Swagger documentation at:
- Swagger UI:
http://localhost:3000/api - JSON Schema:
http://localhost:3000/api-json
The application supports two authentication methods:
- Register:
POST /auth/register - Login:
POST /auth/login - Protected routes require
Authorization: Bearer <token>header
- Initiate:
GET /auth/google - Callback:
GET /auth/google/callback
- Domain Layer (
domain/): Contains business entities and repository interfaces - Application Layer (
application/): Contains use cases and application services - Infrastructure Layer (
infrastructure/): Contains database implementations, external APIs - Presentation Layer (
presentation/): Contains controllers and API endpoints
- Repository Pattern: Data access abstraction
- Dependency Injection: IoC container for loose coupling
- Strategy Pattern: Multiple authentication strategies
- Interceptor Pattern: Cross-cutting concerns (logging, response formatting)
# Development
pnpm start:dev # Start in development mode
pnpm start:debug # Start in debug mode
# Building
pnpm build # Build the application
pnpm start:prod # Start in production mode
# Testing
pnpm test # Run unit tests
pnpm test:watch # Run tests in watch mode
pnpm test:cov # Run tests with coverage
pnpm test:e2e # Run BDD tests
# Code Quality
pnpm lint # Run ESLint
pnpm format # Format code with Prettier
# Database
pnpm prisma generate # Generate Prisma client
pnpm prisma migrate dev # Run database migrations
pnpm prisma studio # Open Prisma Studio| Variable | Description | Required |
|---|---|---|
DATABASE_URL |
PostgreSQL connection string | Yes |
JWT_SECRET |
Secret key for JWT tokens | Yes |
JWT_EXPIRES_IN |
JWT token expiration time | Yes |
GOOGLE_CLIENT_ID |
Google OAuth2 client ID | Yes |
GOOGLE_CLIENT_SECRET |
Google OAuth2 client secret | Yes |
MAIL_HOST |
SMTP server host | Yes |
MAIL_USER |
SMTP username | Yes |
MAIL_PASS |
SMTP password | Yes |
PORT |
Application port | No (default: 3000) |
NODE_ENV |
Environment mode | No (default: development) |
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
This project uses Conventional Commits:
feat: add new feature
fix: fix a bug
docs: update documentation
style: formatting changes
refactor: code refactoring
test: add or update tests
chore: maintenance tasks
This project is licensed under the UNLICENSED License.
If you encounter any issues or have questions, please:
- Check the Issues page
- Create a new issue with detailed information
- Contact the maintainers
- NestJS - Progressive Node.js framework
- Prisma - Next-generation ORM
- Cucumber.js - BDD testing framework
- Passport.js - Authentication middleware
Happy Coding! π