Skip to content

Romarionijim/nestjs-server-app-sample

Repository files navigation

NestJS Server Application

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Discord Backers on Open Collective Sponsors on Open Collective Donate us Support us Follow us on Twitter

Overview

A robust NestJS-based server application that implements a comprehensive authentication system with role-based access control. Built with TypeScript, this application features:

  • User authentication (registration, login)
  • Role-based authorization (admin/user roles)
  • REST API endpoints
  • Automated API testing infrastructure (Playwright + TypeScript)
  • Docker containerization
  • PostgreSQL database integration

Project Structure

.
├── .github/
│   ├── actions/         # Custom GitHub Actions
│   └── workflows/       # CI/CD pipeline configurations
├── api/                 # API testing infrastructure
│   ├── enums/          # Enums for URLs, endpoints, methods, etc.
│   │   ├── application-urls.enum.ts
│   │   ├── endpoints.enum.ts
│   │   ├── request-methods.enum.ts
│   │   ├── status-codes.enum.ts
│   │   └── test-tags.enum.ts
│   ├── fixtures/       # Test fixtures and data setup
│   │   ├── auth.fixture.ts
│   │   ├── global.fixture.ts
│   │   ├── merged-fixtures.fixture.ts
│   │   ├── service.fixture.ts
│   │   └── scripts/
│   ├── helpers/        # Test helpers and factories
│   │   └── factory/
│   ├── infra/         # Test infrastructure
│   │   ├── api-client.ts
│   │   └── services/
│   ├── logger/        # Custom logging
│   │   └── custom.logger.ts
│   ├── tests/         # API test suites
│   │   ├── auth-api/  # Authentication tests
│   │   └── users-api/ # User management tests
│   ├── types/         # TypeScript type definitions
│   │   ├── request.types.ts
│   │   └── user/
│   └── utils/         # Testing utilities
│       └── mocks/
├── src/               # Application source code
│   ├── auth/          # Authentication module
│   ├── users/         # Users module
│   └── middleware/    # Custom middleware
├── docker-compose.yml # Docker compose configuration
└── Dockerfile        # Docker image definition

Features

Authentication

  • User registration with role assignment
  • JWT-based authentication
  • Protected routes with Guards
  • Role-based access control (RBAC)

Testing Infrastructure

  • Playwright for API testing
  • Custom fixtures for test setup
  • Automated test runs with CI/CD
  • Comprehensive API test coverage

Database

  • PostgreSQL integration
  • In-memory database for development
  • Docker container setup

Getting Started

Prerequisites

  • Node.js (v20 or later)
  • Docker and Docker Compose
  • npm/yarn package manager

Installation

# Install dependencies
$ npm ci

# Install Playwright for testing
$ npx playwright install

Development

# Start development server
$ npm run start:dev

Testing

The project includes a comprehensive API testing suite:

# Run API tests
$ npm run test:api

Deployment

This project uses Docker for containerization and deployment:

# Build and start Docker containers
$ docker-compose up -d

# Stop containers
$ docker-compose down

The application includes:

  • NestJS server container
  • PostgreSQL database container
  • pgAdmin container for database management

Environment Variables

Required environment variables:

  • JWT_SECRET: Secret key for JWT tokens
  • PORT: Application port (default: 3000)
  • POSTGRES_PASSWORD: Database password
  • PGADMIN_DEFAULT_EMAIL: pgAdmin email
  • PGADMIN_DEFAULT_PASSWORD: pgAdmin password

API Documentation

Once the application is running, visit http://localhost:3000/api for Swagger documentation.

Main Endpoints

  • Authentication:

    • POST /auth/register - Register new user
    • POST /auth/login - Login user
    • GET /auth/profile - Get user profile (protected)
  • Users:

    • GET /users - List users (protected)
    • GET /users/:id - Get user by ID (protected)
    • PATCH /users/:id - Update user (protected)
    • DELETE /users/:id - Delete user (protected)

GitHub Actions CI/CD

This project uses GitHub Actions for continuous integration and deployment. The pipeline includes:

Automated Testing

  • Triggers on:
    • Push to main branch
    • Pull requests to main branch
    • Manual workflow dispatch
  • Runs API tests in parallel for different test suites (@users, @auth)
  • Uses Docker Compose for test environment setup
  • Validates server health before running tests

Continuous Deployment

  • Automatically deploys to Docker Hub on successful main branch pushes
  • Requires successful test completion
  • Builds and pushes latest Docker image

Contributing

  1. Create a feature branch
  2. Commit your changes
  3. Push to the branch
  4. Create a Pull Request

About

small server app developed in nestjs along with a REST API test automation framework

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •