Skip to content

Repository files navigation

Gym Fitness API

A REST API for managing gym and fitness-related data, built with Node.js, Express.js, PostgreSQL, and Docker.

The API provides structured endpoints for retrieving and managing fitness data, with support for filtering, sorting, pagination, request validation, database seeding, and consistent error handling.

Key Features

  • RESTful API architecture
  • PostgreSQL database integration
  • Filtering and search support
  • Sorting and pagination
  • Request validation
  • Structured error responses
  • Database schema and seed scripts
  • Health-check endpoint
  • Docker-based local environment
  • Postman collection for API testing

Technology Stack

  • Node.js
  • Express.js
  • PostgreSQL
  • SQL
  • Docker
  • Docker Compose
  • Zod
  • JavaScript
  • Postman

Prerequisites

Make sure the following tools are installed:

  • Node.js
  • npm
  • Docker
  • Docker Compose
  • PostgreSQL, when running without Docker

Clone the Repository

git clone https://github.com/maaz2692/gym-fitness-api.git
cd gym-fitness-api

Install Dependencies

npm install

Environment Configuration

Create a local environment file from the example:

Windows PowerShell

Copy-Item .env.example .env

macOS or Linux

cp .env.example .env

Update the values inside .env according to your local environment.

Example:

PORT=3000
DB_HOST=localhost
DB_PORT=5432
DB_NAME=gym_fitness
DB_USER=your_database_user
DB_PASSWORD=your_database_password

Never commit the .env file or real database credentials to the repository.

Running with Docker

Start the API and PostgreSQL services:

docker compose up --build

To stop the services:

docker compose down

To remove the containers and associated volumes:

docker compose down -v

Running Locally

Start the development server:

npm run dev

When a development script is not configured, use:

npm start

The API will run on:

http://localhost:3000

Database Setup

Run the database schema script before starting the application when PostgreSQL is running locally.

Example:

psql -U your_database_user -d gym_fitness -f database/schema.sql

Load sample data using the seed script:

psql -U your_database_user -d gym_fitness -f database/seed.sql

Adjust the file paths if the database scripts are stored in a different folder.

API Capabilities

The API supports:

  • Retrieving fitness records
  • Filtering results using query parameters
  • Sorting results by supported fields
  • Paginating large result sets
  • Validating incoming request data
  • Returning structured success and error responses

Example request:

GET /api/resources?page=1&limit=10&sort=name

Replace /api/resources with the actual route used by the project.

Health Check

Use the health endpoint to verify that the API is running:

GET /health

Example response:

{
  "status": "ok"
}

Update this section if your health-check route or response is different.

API Testing with Postman

A Postman collection is included in the repository.

To use it:

  1. Open Postman.
  2. Select Import.
  3. Choose the collection file from the repository.
  4. Configure the base URL.
  5. Run the available requests.

Example base URL:

http://localhost:3000

Project Structure

.
├── src/
│   ├── controllers/       # Request and response handling
│   ├── routes/            # API route definitions
│   ├── services/          # Business logic
│   ├── middleware/        # Validation and error handling
│   ├── validators/        # Request validation schemas
│   ├── database/          # Database connection and queries
│   └── app.js             # Express application setup
├── database/
│   ├── schema.sql         # Database schema
│   └── seed.sql           # Sample data
├── postman/               # Postman collection
├── .env.example
├── Dockerfile
├── docker-compose.yml
├── package.json
└── README.md

Change this structure so that it matches the repository exactly.

Validation and Error Handling

Incoming requests are validated before being processed.

Example validation error:

{
  "error": "Validation failed",
  "details": [
    {
      "field": "name",
      "message": "Name is required"
    }
  ]
}

The actual error format may differ depending on the project implementation.

Future Improvements

  • Migrate the codebase to TypeScript
  • Add automated unit and integration tests
  • Add Swagger or OpenAPI documentation
  • Add authentication and role-based authorization
  • Add GitHub Actions for continuous integration
  • Deploy the API publicly
  • Add structured application logging
  • Add rate limiting and security headers

Author

Maaz Mustafa Khurshed Qazi

License

This project is available for educational and portfolio purposes.

About

REST API for managing fitness data, built with Node.js, Express, PostgreSQL, Zod and Docker.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages