Robust REST API Backend for the Gym Engine Platform
Base URL: https://gym-engine-server.vercel.app/api/v1
The Gym Engine Server is a fully typed, production-ready REST API built with Express 5 and TypeScript. It powers all platform operations β authentication, user management, class scheduling, bookings, and role-based access control β for the Gym Engine frontend.
gym-engine-server/
β
βββ index.ts # Entry point β bootstraps app & DB connection
β
βββ src/
β βββ config/
β β βββ db.ts # MongoDB connection setup
β β
β βββ controllers/
β β βββ auth.controller.ts
β β βββ user.controller.ts
β β βββ class.controller.ts
β β βββ schedule.controller.ts
β β βββ booking.controller.ts
β β
β βββ models/
β β βββ user.model.ts
β β βββ class.model.ts
β β βββ schedule.model.ts
β β βββ booking.model.ts
β β
β βββ routes/
β β βββ auth.routes.ts
β β βββ user.routes.ts
β β βββ class.routes.ts
β β βββ schedule.routes.ts
β β βββ booking.routes.ts
β β
β βββ middlewares/
β β βββ auth.middleware.ts # JWT verification
β β βββ role.middleware.ts # RBAC enforcement
β β βββ error.middleware.ts # Global error handler
β β
β βββ utils/
β βββ generateToken.ts
β βββ responseHelper.ts
β
βββ dist/ # Compiled JS output (auto-generated)
βββ .env # Environment variables
βββ tsconfig.json
βββ package.json
| Package | Version | Purpose |
|---|---|---|
express |
^5.2.1 | HTTP server & routing framework |
mongodb |
^7.2.0 | MongoDB driver for database ops |
jsonwebtoken |
^9.0.3 | JWT creation & verification |
bcryptjs |
^3.0.3 | Password hashing & comparison |
cors |
^2.8.6 | Cross-Origin Resource Sharing |
cookie-parser |
^1.4.7 | Parse cookies from requests |
dotenv |
^17.4.2 | Load environment variables |
morgan |
^1.10.1 | HTTP request logger |
| Package | Version | Purpose |
|---|---|---|
typescript |
^6.0.3 | Type-safe JavaScript superset |
ts-node-dev |
^2.0.0 | Dev server with hot reload |
@types/express |
^5.0.6 | Express type definitions |
@types/node |
^25.6.0 | Node.js type definitions |
@types/jsonwebtoken |
^9.0.10 | JWT type definitions |
@types/bcryptjs |
^2.4.6 | bcryptjs type definitions |
@types/cors |
^2.8.19 | CORS type definitions |
@types/morgan |
^1.9.10 | Morgan type definitions |
@types/cookie-parser |
^1.4.10 | Cookie-parser type definitions |
- Node.js v18+
- MongoDB (Atlas or local)
- npm or yarn
git clone https://github.com/tuhin360/gym-engine-server.git
cd gym-engine-servernpm installCreate a .env file in the root:
# Server
PORT=5000
# Database
MONGO_URI=mongodb+srv://<user>:<password>@cluster.mongodb.net/gym-engine
# Auth
JWT_SECRET=your_super_secret_jwt_key
# Frontend
FRONTEND_URL=http://localhost:3000# Development (hot reload via ts-node-dev)
npm run dev
# Production (compile β run)
npm startServer starts at
http://localhost:5000
| Script | Command | Description |
|---|---|---|
dev |
ts-node-dev --respawn --transpile-only index.ts |
Dev server with hot reload |
start |
tsc && node dist/index.js |
Compile TypeScript then run |
test |
(not yet configured) | Test runner placeholder |
| Method | Endpoint | Access | Description |
|---|---|---|---|
POST |
/register |
Public | Register a new user |
POST |
/login |
Public | Login & receive JWT |
POST |
/logout |
Auth | Invalidate session |
GET |
/me |
Auth | Get current user profile |
| Method | Endpoint | Access | Description |
|---|---|---|---|
GET |
/ |
Admin | Get all users |
GET |
/:id |
Admin/Self | Get user by ID |
PATCH |
/:id |
Admin/Self | Update user details |
DELETE |
/:id |
Admin | Delete a user |
PATCH |
/:id/role |
Admin | Update user role |
| Method | Endpoint | Access | Description |
|---|---|---|---|
GET |
/ |
Public | Get all classes |
GET |
/:id |
Public | Get class by ID |
POST |
/ |
Admin/Trainer | Create new class |
PATCH |
/:id |
Admin/Trainer | Update class |
DELETE |
/:id |
Admin | Delete class |
| Method | Endpoint | Access | Description |
|---|---|---|---|
GET |
/ |
Public | Get all schedules |
POST |
/ |
Admin/Trainer | Create schedule slot |
PATCH |
/:id |
Admin/Trainer | Update schedule |
DELETE |
/:id |
Admin | Delete schedule |
| Method | Endpoint | Access | Description |
|---|---|---|---|
GET |
/ |
Admin | Get all bookings |
GET |
/my |
Member | Get own bookings |
POST |
/ |
Member | Create a booking |
DELETE |
/:id |
Member/Admin | Cancel a booking |
Client Server
β β
ββββ POST /auth/login βββββββββΆβ
β { email, password } β
β βββ bcryptjs.compare()
β βββ JWT sign (role + id)
ββββ { token, user } ββββββββββ
β β
ββββ GET /protected βββββββββββΆβ
β Authorization: Bearer βββ verifyToken middleware
β βββ checkRole middleware
ββββ { protected data } βββββββ
Request
β
βΌ
morgan() β Logs method, URL, status, response time
cors() β Validates origin against FRONTEND_URL
express.json() β Parses JSON request bodies
cookieParser() β Parses cookies (refresh tokens etc.)
β
βΌ
Route Handler
β
βΌ
authMiddleware β Verifies JWT from Authorization header
roleMiddleware β Enforces role-based access (Admin / Trainer / Member)
β
βΌ
Controller Logic
β
βΌ
errorMiddleware β Catches thrown errors, formats JSON error response
{
_id: ObjectId,
name: string,
email: string, // unique
password: string, // bcrypt hashed
role: "admin" | "trainer" | "member" | "non-member",
createdAt: Date,
updatedAt: Date
}{
_id: ObjectId,
title: string,
description: string,
category: "basic" | "yoga" | "bodybuilding" | "muscle",
trainer: ObjectId, // ref β User
capacity: number,
createdAt: Date
}{
_id: ObjectId,
class: ObjectId, // ref β Class
trainer: ObjectId, // ref β User
date: Date,
startTime: string,
endTime: string,
slots: number
}{
_id: ObjectId,
user: ObjectId, // ref β User
schedule: ObjectId, // ref β Schedule
status: "confirmed" | "cancelled",
bookedAt: Date
}{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"outDir": "./dist",
"rootDir": "./",
"skipLibCheck": true
},
"include": ["**/*.ts"],
"exclude": ["node_modules", "dist"]
}- β Passwords hashed with bcryptjs (salt rounds: 10+)
- β Auth via short-lived JWT tokens
- β Role enforcement on every protected route
- β CORS restricted to known frontend origin
- β Environment secrets loaded via dotenv (never committed)
- β All inputs validated before database operations
The server is deployed on Vercel using serverless functions.
# Install Vercel CLI
npm i -g vercel
# Deploy
vercel --prodMake sure all environment variables from .env are added to your Vercel project settings.
- JWT authentication & role-based access
- Class & schedule management APIs
- Booking system
- Morgan logging
- π WebSocket notifications (Socket.io)
- π¬ TrainerβMember messaging API
- π Advanced analytics endpoints
- β Jest unit & integration tests
- π³ Dockerize for local dev
- π Refresh token rotation
β Star this repo if it helped you β it keeps the project alive!
Made with β€οΈ by Jahedi Alam Tuhin