Skip to content

huanchain/nest-backend-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

NestJS Backend API

A comprehensive NestJS backend API with authentication, Prisma ORM, PostgreSQL database, and Swagger documentation.

πŸš€ Features

  • Node.js 22 - Latest LTS version
  • NestJS Framework - Progressive Node.js framework
  • Prisma ORM - Next-generation ORM for Node.js and TypeScript
  • PostgreSQL Database - Robust relational database
  • JWT Authentication - Secure token-based authentication
  • Swagger Documentation - Interactive API documentation
  • Unit Testing - Comprehensive test coverage with Jest
  • TypeScript - Full TypeScript support
  • Validation - Request validation with class-validator
  • CORS Support - Cross-origin resource sharing enabled

πŸ“‹ Prerequisites

  • Node.js 22 or higher
  • PostgreSQL database
  • npm or yarn package manager

πŸ› οΈ Installation

  1. Clone the repository

    git clone <repository-url>
    cd nest-backend-api
  2. Install dependencies

    yarn install
  3. Environment Setup

    cp env.example .env

    Update the .env file with your database credentials:

    DATABASE_URL="postgresql://username:password@localhost:5432/database_name"
    JWT_SECRET="your-super-secret-jwt-key"
    JWT_EXPIRES_IN="7d"
    PORT=3000
  4. Database Setup

    # Generate Prisma client
    yarn prisma:generate
    
    # Push schema to database
    yarn prisma:push
    
    # Or run migrations
    yarn prisma:migrate
  5. Start the application

    # Development mode
    yarn start:dev
    
    # Production mode
    yarn build
    yarn start:prod

πŸ“ Project Structure

nest-backend-api/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ auth/                    # Authentication module
β”‚   β”‚   β”œβ”€β”€ dto/                 # Data Transfer Objects
β”‚   β”‚   β”‚   β”œβ”€β”€ register.dto.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ login.dto.ts
β”‚   β”‚   β”‚   └── auth-response.dto.ts
β”‚   β”‚   β”œβ”€β”€ guards/              # Authentication guards
β”‚   β”‚   β”‚   β”œβ”€β”€ jwt-auth.guard.ts
β”‚   β”‚   β”‚   └── local-auth.guard.ts
β”‚   β”‚   β”œβ”€β”€ strategies/          # Passport strategies
β”‚   β”‚   β”‚   β”œβ”€β”€ jwt.strategy.ts
β”‚   β”‚   β”‚   └── local.strategy.ts
β”‚   β”‚   β”œβ”€β”€ auth.controller.ts   # Authentication endpoints
β”‚   β”‚   β”œβ”€β”€ auth.service.ts      # Authentication business logic
β”‚   β”‚   └── auth.module.ts       # Authentication module
β”‚   β”œβ”€β”€ users/                   # Users module
β”‚   β”‚   β”œβ”€β”€ users.controller.ts  # User endpoints
β”‚   β”‚   β”œβ”€β”€ users.service.ts     # User business logic
β”‚   β”‚   └── users.module.ts      # User module
β”‚   β”œβ”€β”€ prisma/                  # Database module
β”‚   β”‚   β”œβ”€β”€ prisma.service.ts    # Prisma client service
β”‚   β”‚   └── prisma.module.ts     # Prisma module
β”‚   β”œβ”€β”€ common/                  # Shared utilities
β”‚   β”‚   β”œβ”€β”€ decorators/          # Custom decorators
β”‚   β”‚   β”œβ”€β”€ guards/              # Global guards
β”‚   β”‚   β”œβ”€β”€ interceptors/        # Global interceptors
β”‚   β”‚   β”œβ”€β”€ pipes/               # Global pipes
β”‚   β”‚   └── utils/               # Utility functions
β”‚   β”œβ”€β”€ config/                  # Configuration files
β”‚   β”œβ”€β”€ app.controller.ts        # Main app controller
β”‚   β”œβ”€β”€ app.service.ts           # Main app service
β”‚   β”œβ”€β”€ app.module.ts            # Main app module
β”‚   └── main.ts                  # Application entry point
β”œβ”€β”€ prisma/
β”‚   └── schema.prisma            # Database schema
β”œβ”€β”€ test/                        # E2E tests
β”‚   β”œβ”€β”€ app.e2e-spec.ts
β”‚   └── jest-e2e.json
β”œβ”€β”€ package.json
β”œβ”€β”€ tsconfig.json
β”œβ”€β”€ nest-cli.json
└── README.md

πŸ”§ API Endpoints

Authentication

Method Endpoint Description Auth Required
POST /auth/register Register a new user No
POST /auth/login Login user No
GET /auth/profile Get user profile Yes

Users

Method Endpoint Description Auth Required
GET /users/profile Get current user profile Yes

General

Method Endpoint Description Auth Required
GET / API health check No
GET /health Health status No

πŸ“š API Documentation

Once the application is running, you can access the Swagger documentation at:

πŸ§ͺ Testing

Run Tests

# Unit tests
yarn test

# E2E tests
yarn test:e2e

# Test coverage
yarn test:cov

# Watch mode
yarn test:watch

Test Structure

  • Unit Tests: Located in *.spec.ts files alongside source code
  • E2E Tests: Located in the test/ directory
  • Coverage: Generated in the coverage/ directory

πŸ—„οΈ Database

Prisma Commands

# Generate Prisma client
yarn prisma:generate

# Push schema changes to database
yarn prisma:push

# Create and run migrations
yarn prisma:migrate

# Open Prisma Studio (database GUI)
yarn prisma:studio

Database Schema

The application uses a simple User model:

model User {
  id        String   @id @default(cuid())
  email     String   @unique
  password  String
  name      String?
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

πŸ” Authentication

The API uses JWT (JSON Web Tokens) for authentication:

  1. Register: Create a new user account
  2. Login: Authenticate and receive a JWT token
  3. Protected Routes: Include the JWT token in the Authorization header

Example Usage

# Register
curl -X POST http://localhost:3000/auth/register \
  -H "Content-Type: application/json" \
  -d '{"email":"user@example.com","password":"password123","name":"John Doe"}'

# Login
curl -X POST http://localhost:3000/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email":"user@example.com","password":"password123"}'

# Access protected route
curl -X GET http://localhost:3000/auth/profile \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

πŸš€ Deployment

Environment Variables

Make sure to set the following environment variables in production:

DATABASE_URL="your-production-database-url"
JWT_SECRET="your-production-jwt-secret"
JWT_EXPIRES_IN="7d"
PORT=3000
NODE_ENV="production"

Build for Production

yarn build
yarn start:prod

πŸ“ Available Scripts

Script Description
yarn build Build the application
yarn start Start the application
yarn start:dev Start in development mode with hot reload
yarn start:debug Start in debug mode
yarn start:prod Start in production mode
yarn test Run unit tests
yarn test:watch Run tests in watch mode
yarn test:cov Run tests with coverage
yarn test:e2e Run e2e tests
yarn lint Run ESLint
yarn format Format code with Prettier

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License.

πŸ†˜ Support

If you have any questions or need help, please open an issue in the repository.


Happy Coding! πŸŽ‰

About

NestJS Backend API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors