A modern NestJS backend template with Nx build system, featuring modular architecture and scalable services.
- Backend Template Project
- NestJS Framework: Modern Node.js framework
- TypeScript: Strong typing
- Nx Build System: Monorepo support
- Database:
- PostgreSQL (production)
- SQLite (development)
- Easy DB switching via env vars
- Auth: JWT + Passport
- API Docs: Swagger/OpenAPI
- Caching: Redis + @nestjs/cache-manager
- Queue: BullMQ
- Testing: Jest (unit/E2E) + Vitest (unit advanced)
- Docker: Dev/Prod configurations
- Node.js >= 22.15.1
- Yarn >= 4.9.1
- PostgreSQL >= 17 or SQLite3
- Redis >= 6.2.0
- Docker and Docker Compose
# Install dependencies
yarn install
# Set up environment variables
cp .env.example .env
# Seed the database
yarn seedπ‘ Note: The
yarn seedcommand populatesdatabase.sqlite3for development. This file is shared with Docker via volume, so Docker will use the same seeded database when running in development mode.
If you want to run the app without full Docker (e.g., just locally with yarn start) but don't have Redis installed:
# Start Redis via Docker
docker compose up -d redis
# Then start the app locally
yarn startπ‘ Note: The development environment uses SQLite3 for lighter setup. More here.
# Run the development environment
docker compose up -dThis will launch:
- the application
- Redis
- and use the pre-seeded
database.sqlite3file
.
βββ apps/
β βββ service-a/ # Main app
β βββ service-a-e2e/ # E2E tests
βββ libs/
β βββ modules/ # Shared modules
β β βββ auth/ # Auth
β β βββ ... # Various modules
β βββ entities/ # Database entities
β βββ db/ # Database configuration
β βββ utils/ # Shared helpers & utility functions
βββ docs/ # Documentation
βββ config files # Various configs
yarn start # Run dev server
yarn build # Build app
yarn seed # Seed DB
yarn test:unit # Unit tests
yarn test:e2e # E2E tests# App
PORT=3000
# Database Configuration
DB_TYPE=postgres # or sqlite
SQLITE_STORAGE=./database.sqlite3 # Only used when DB_TYPE=sqlite
# PostgreSQL Configuration (when DB_TYPE=postgres)
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=postgres
DB_DATABASE=app_db
# JWT
JWT_SECRET=your-secret
JWT_EXPIRATION=1h
# Redis
REDIS_HOST=localhost
REDIS_PORT=6379The project supports both PostgreSQL and SQLite databases. You can switch between them using the DB_TYPE environment variable:
DB_TYPE=postgres
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=postgres
DB_DATABASE=app_dbDB_TYPE=sqlite
SQLITE_STORAGE=./database.sqlite3-
Unit Tests: Jest & Vitest (
*.spec.ts)- Run with
yarn test:unit
- Run with
-
E2E Tests: Jest (
apps/service-a-e2e)- Run with
yarn test:e2e
- Run with
-
Coverage: Reports in
coverage/- Use flag
--coveragewhen running tests
- Use flag
βΉοΈ Note: If you're running
yarn test:e2e, consider using a dedicated.env.testfile instead of.env.local. This ensures you do not accidentally seed or connect to your development or production database during E2E tests.
You can do this by copying and editing the default env:
cp .env.example .env.testThen configure .env.test to use SQLite or a separate test database.
http://localhost:3000
For step-by-step instructions on registering, logging in, and using protected endpoints, see the API Usage Guide.
Features:
- Interactive API documentation
- Request/response examples
- Authentication support
- Downloadable OpenAPI specification:
http://localhost:3000/openapi.json
-
Authentication:
- JWT-based authentication with Passport.js
- Secure password hashing with bcrypt
- Protected routes using
JwtAuthGuard
-
Data Protection:
- Environment variable management
- Input validation using
class-validatorand NestJSValidationPipe:- Auto-transforms inputs to expected types
- Whitelists only allowed fields
- Forbids unexpected properties
- Secure session/token handling
- SQL injection prevention
-
App-level Security:
helmetfor setting HTTP headers (e.g., disablingX-Powered-By, frameguard, XSS protection)- Rate limiting per IP (default: 100 requests/minute)
- CORS enabled with domain restriction via
CORS_ORIGINenvironment variable - Trust proxy setup for reverse proxies (e.g., Nginx)
The project includes Docker configuration for development and production:
docker compose up -dServices:
- Application service
- SQLite
- Redis for caching and queues
docker compose -f docker-compose.production.yml up -dServices:
- Application service
- PostgreSQL
- Redis for caching and queues
@nestjs/*: Core NestJS packagessequelize: ORM for database operationsbullmq: Queue managementpassport: Authenticationclass-validator: Input validationswagger: API documentation
nx: Build systemjest&vitest: Testing frameworkseslint: Code lintingprettier: Code formattingtypescript: TypeScript support
-
Infrastructure
- Add Kubernetes deployment configuration
- Add Terraform setup for infrastructure provisioning
-
API Testing
- Expand Postman collection with full test coverage
- Automate Postman workflows using Newman (CI-ready)
-
Architecture
- Refactor project structure for microservices support
- Extract Auth and Transactions into separate services
-
DevOps / CI-CD
- Add GitHub Actions / GitLab CI pipelines
- Integrate Docker image build & push
-
Monitoring & Observability
- Add logging (e.g., Winston / Pino)
- Setup Prometheus + Grafana or another monitoring stack
-
Security Enhancements
- Add rate limiting per endpoint group
- Add CSRF/XSS protection for future frontend interactions