A production-ready NestJS API with best practices, authentication, database integration, file uploads with AWS S3, and comprehensive documentation.
- Authentication & Authorization: JWT-based auth with Passport.js
- Database Integration: TypeORM with PostgreSQL
- File Uploads: AWS S3 integration with image and document uploads
- Content Management: Complete CMS with posts, categories, and featured images
- API Documentation: Swagger/OpenAPI integration
- Validation: Class-validator with transformation
- Security: Helmet, CORS, rate limiting
- Error Handling: Global exception filters
- Logging: Structured logging with interceptors
- Testing: Unit and E2E tests setup
- Docker: Multi-stage builds with development and production configurations
- Environment Configuration: Multiple environment support
- Code Quality: ESLint, Prettier, Husky hooks
- Node.js >= 18
- PostgreSQL >= 13
- AWS Account with S3 access (for file uploads)
- Docker & Docker Compose (optional)
-
Clone and install dependencies
cd api npm install
-
Environment setup
cp .env.development .env # Edit .env with your local configuration including S3 settings
-
AWS S3 Configuration (Required for file uploads)
# Add to your .env file: AWS_ACCESS_KEY_ID=your-access-key-id AWS_SECRET_ACCESS_KEY=your-secret-access-key AWS_REGION=us-east-1 AWS_S3_BUCKET=your-bucket-name AWS_S3_PUBLIC_URL=https://your-bucket-name.s3.amazonaws.com
π Detailed S3 setup guide: docs/S3_INTEGRATION.md
-
Database setup
# Start PostgreSQL (or use Docker) npm run start:dev
# Start all services
docker-compose up -d
# View logs
docker-compose logs -f api
# Stop services
docker-compose down
npm run start:dev
npm run build
npm run start:prod
docker-compose -f docker-compose.prod.yml up -d
Once running, visit:
- Swagger UI: http://localhost:3000/docs
- API Endpoints: http://localhost:3000/api/v1
POST /api/v1/auth/register
- Register new userPOST /api/v1/auth/login
- User loginGET /api/v1/auth/profile
- Get user profile (Protected)
POST /api/v1/posts
- Create post (with file upload)GET /api/v1/posts
- List posts (with filtering & pagination)GET /api/v1/posts/published
- Published posts onlyGET /api/v1/posts/:id
- Get post by IDGET /api/v1/posts/slug/:slug
- Get post by slugPATCH /api/v1/posts/:id
- Update postDELETE /api/v1/posts/:id
- Delete postPATCH /api/v1/posts/:id/publish
- Publish postPATCH /api/v1/posts/:id/unpublish
- Unpublish postPATCH /api/v1/posts/:id/archive
- Archive post
POST /api/v1/categories
- Create categoryGET /api/v1/categories
- List categories (tree structure)GET /api/v1/categories/:id
- Get category by IDGET /api/v1/categories/slug/:slug
- Get category by slugPATCH /api/v1/categories/:id
- Update categoryDELETE /api/v1/categories/:id
- Delete category
POST /api/v1/uploads/image
- Upload image file (jpg, png, webp)POST /api/v1/uploads/file
- Upload any file typePOST /api/v1/uploads/files
- Upload multiple filesDELETE /api/v1/uploads/file
- Delete file from S3
GET /api/v1/files/:folder/:filename
- Serve files via API proxy
GET /api/v1/users
- Get all users (Paginated)GET /api/v1/users/:id
- Get user by IDPOST /api/v1/users
- Create userPATCH /api/v1/users/:id
- Update userDELETE /api/v1/users/:id
- Delete user
# Generate migration
npm run migration:generate -- src/database/migrations/MigrationName
# Run migrations
npm run migration:run
# Revert migration
npm run migration:revert
npm run seed
# Unit tests
npm run test
# E2E tests
npm run test:e2e
# Test coverage
npm run test:cov
# Watch mode
npm run test:watch
npm run build # Build the application
npm run start # Start the application
npm run start:dev # Start in development mode
npm run start:prod # Start in production mode
npm run lint # Lint the code
npm run format # Format the code
npm run test # Run tests
npm run test:e2e # Run E2E tests
src/
βββ common/ # Shared utilities
β βββ decorators/ # Custom decorators
β βββ dto/ # Data Transfer Objects
β βββ filters/ # Exception filters
β βββ guards/ # Authentication guards
β βββ interceptors/ # Request/Response interceptors
βββ config/ # Configuration files
βββ database/ # Database related files
βββ modules/ # Feature modules
β βββ auth/ # Authentication module
β βββ users/ # Users module
βββ main.ts # Application entry point
# Database
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=password
DATABASE_NAME=nestjs_api
# JWT
JWT_SECRET=your-super-secret-jwt-key
JWT_EXPIRES_IN=7d
# Application
PORT=3000
NODE_ENV=development
API_PREFIX=api/v1
CORS_ORIGIN=http://localhost:3000
# Rate Limiting
THROTTLE_TTL=60
THROTTLE_LIMIT=10
- Helmet: Security headers
- CORS: Cross-origin resource sharing
- Rate Limiting: Request throttling
- Input Validation: Request validation and sanitization
- JWT Authentication: Secure token-based auth
- Password Hashing: bcrypt for password security
npm run build
npm run start:prod
docker build -t nestjs-api .
docker run -p 3000:3000 nestjs-api
- Development:
docker-compose up
- Production:
docker-compose -f docker-compose.prod.yml up
- Fork the repository
- Create a feature branch:
git checkout -b feature/new-feature
- Commit changes:
git commit -am 'Add new feature'
- Push to branch:
git push origin feature/new-feature
- Submit a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- NestJS - Progressive Node.js framework
- TypeORM - ORM for TypeScript
- Passport - Authentication middleware
Nest framework TypeScript starter repository.
$ npm install
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
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:
$ npm 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.
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.