Skip to content

tuanano/nest-codebase

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

NestJS API - Production Ready

A production-ready NestJS API with best practices, authentication, database integration, file uploads with AWS S3, and comprehensive documentation.

πŸš€ Features

  • 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

πŸ“‹ Prerequisites

  • Node.js >= 18
  • PostgreSQL >= 13
  • AWS Account with S3 access (for file uploads)
  • Docker & Docker Compose (optional)

πŸ› οΈ Installation

Local Development

  1. Clone and install dependencies

    cd api
    npm install
  2. Environment setup

    cp .env.development .env
    # Edit .env with your local configuration including S3 settings
  3. 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

  4. Database setup

    # Start PostgreSQL (or use Docker)
    npm run start:dev

Docker Development

# Start all services
docker-compose up -d

# View logs
docker-compose logs -f api

# Stop services
docker-compose down

πŸƒβ€β™‚οΈ Running the Application

Development

npm run start:dev

Production

npm run build
npm run start:prod

Docker Production

docker-compose -f docker-compose.prod.yml up -d

πŸ“– API Documentation

Once running, visit:

πŸ”— Available Endpoints

Authentication

  • POST /api/v1/auth/register - Register new user
  • POST /api/v1/auth/login - User login
  • GET /api/v1/auth/profile - Get user profile (Protected)

Posts (CMS Content)

  • 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 only
  • GET /api/v1/posts/:id - Get post by ID
  • GET /api/v1/posts/slug/:slug - Get post by slug
  • PATCH /api/v1/posts/:id - Update post
  • DELETE /api/v1/posts/:id - Delete post
  • PATCH /api/v1/posts/:id/publish - Publish post
  • PATCH /api/v1/posts/:id/unpublish - Unpublish post
  • PATCH /api/v1/posts/:id/archive - Archive post

Categories (Content Organization)

  • POST /api/v1/categories - Create category
  • GET /api/v1/categories - List categories (tree structure)
  • GET /api/v1/categories/:id - Get category by ID
  • GET /api/v1/categories/slug/:slug - Get category by slug
  • PATCH /api/v1/categories/:id - Update category
  • DELETE /api/v1/categories/:id - Delete category

File Uploads (AWS S3)

  • POST /api/v1/uploads/image - Upload image file (jpg, png, webp)
  • POST /api/v1/uploads/file - Upload any file type
  • POST /api/v1/uploads/files - Upload multiple files
  • DELETE /api/v1/uploads/file - Delete file from S3

File Serving

  • GET /api/v1/files/:folder/:filename - Serve files via API proxy

Users

  • GET /api/v1/users - Get all users (Paginated)
  • GET /api/v1/users/:id - Get user by ID
  • POST /api/v1/users - Create user
  • PATCH /api/v1/users/:id - Update user
  • DELETE /api/v1/users/:id - Delete user

πŸ—„οΈ Database

Migrations

# Generate migration
npm run migration:generate -- src/database/migrations/MigrationName

# Run migrations
npm run migration:run

# Revert migration
npm run migration:revert

Seeds

npm run seed

πŸ§ͺ Testing

# Unit tests
npm run test

# E2E tests
npm run test:e2e

# Test coverage
npm run test:cov

# Watch mode
npm run test:watch

πŸ”§ Scripts

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

πŸ“ Project Structure

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

🌍 Environment Variables

Required

# 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

Optional

# Rate Limiting
THROTTLE_TTL=60
THROTTLE_LIMIT=10

πŸ”’ Security Features

  • 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

πŸ“¦ Deployment

Production Build

npm run build
npm run start:prod

Docker Production

docker build -t nestjs-api .
docker run -p 3000:3000 nestjs-api

Environment-specific deployments

  • Development: docker-compose up
  • Production: docker-compose -f docker-compose.prod.yml up

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/new-feature
  3. Commit changes: git commit -am 'Add new feature'
  4. Push to branch: git push origin feature/new-feature
  5. Submit a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • NestJS - Progressive Node.js framework
  • TypeORM - ORM for TypeScript
  • Passport - Authentication middleware

Description

Nest framework TypeScript starter repository.

Project setup

$ npm install

Compile and run the project

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Run tests

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

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:

$ 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.

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

Code Base API for NestJS

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published