A high-performance, robust RESTful API built with Express.js (v5), TypeScript, and MongoDB (via Mongoose), optimized to run on the Bun runtime.
- Bun Runtime Integration: Super-fast execution, module resolution, and built-in hot reloading.
- Premium Security: Protected by
helmet,cors, and IP-based rate limiting viaexpress-rate-limit. - Secure Authentication: Dual-token architecture (Access + Refresh JWT tokens via
jose) with secure HTTP-only cookies. - Structural Validation: Request body validation using
zodschemas prior to controller execution. - Database Seeds: Built-in seeding scripts for fast database initialization (services, leaders, page content, site settings).
- Custom Logger: Automatic request logging with
morganand structured file/console logging with colorized severity highlights (chalk). - Containerization Ready: Complete
Dockerfileandcompose.yamlconfiguration for seamless local development and deployment.
backend/
├── src/
│ ├── server.ts # Application entrypoint & DB connection
│ ├── app.ts # Express application configuration & global middleware
│ └── app/
│ ├── config/ # DB connectivity & Environment configuration
│ ├── controllers/ # Request handlers & Business logic
│ ├── logs/ # Persistent log files (app.log)
│ ├── middlewares/ # Custom middleware (auth validation, rate limiters)
│ ├── models/ # Mongoose schemas/models
│ ├── routes/ # API endpoint routing declarations
│ ├── schemas/ # Zod validation schemas
│ ├── seeds/ # Seed scripts for initial setup
│ ├── services/ # Core third-party integrations & operations
│ ├── types/ # Custom TypeScript type declarations
│ └── utils/ # Helper utilities (standard response wrappers, loggers)
├── dist/ # Compiled JavaScript output
├── Dockerfile # Containerization setup
└── compose.yaml # Docker Compose configuration
- Runtime: Bun (v1.3.1 or higher)
- Framework: Express (v5.x)
- Language: TypeScript (target
esnext, modulenodenext) - Database: MongoDB (via Mongoose ODM)
- Validation: Zod
- Security & Utilities:
jose(JWT signing and verification)bcryptjs(Password hashing)cookie-parser(Cookie management)cors&helmet(Security headers and CORS policy)multer(File upload handling)express-rate-limit(DDoS/Brute-force protection)chalk&morgan(Rich logging)
Copy the .env.example file to .env in the root of the backend folder and fill in the required details:
cp .env.example .env| Variable | Description | Default / Example |
|---|---|---|
NODE_ENV |
Runtime environment mode | development |
SAME_SITE |
Cookie SameSite attribute (lax, none, etc.) |
lax |
PORT |
Listening port for the server | 5000 |
CLIENT_URL |
Base URL of the client application | http://localhost:3000 |
DB_USERNAME |
Database username | your_username |
DB_PASSWORD |
Database password | your_password |
DB_CLUSTER |
Database cluster domain | cluster0.mongodb.net |
DB_NAME |
Target MongoDB database name | db_name |
DB_APP_NAME |
MongoDB connection application name (appName) | appname |
ACCESS_TOKEN_SECRET |
Secret key for JWT Access Token generation | Super secret key |
ACCESS_TOKEN_EXPIRATION |
Expiry duration for Access Token | 15m |
REFRESH_TOKEN_SECRET |
Secret key for JWT Refresh Token generation | Super secret key |
REFRESH_TOKEN_EXPIRATION |
Expiry duration for Refresh Token | 7d |
HASH_SALT_ROUNDS |
Cryptographic salt rounds for password hashing | 10 |
- Bun (Recommended) or Node.js (v18+):
- Bun:
- macOS/Linux:
curl -fsSL https://bun.sh/install | bash - Windows (PowerShell):
powershell -c "irm bun.sh/install.ps1 | iex"
- macOS/Linux:
- Node.js:
- Download and install from nodejs.org.
- Bun:
- Navigate to the backend directory:
cd backend - Install dependencies:
bun install # or npm install
Runs the development server:
bun run dev
# or
npm run devCompiles/builds and starts the production server using Node.js:
npm run build
npm startTo populate your database with initial mock data (Services, Leaders, Page Content, and Site Settings):
bun run seed
# or
npm run seedYou can run the backend along with the entire environment using Docker:
-
Start the Container:
docker compose up --build
This loads the
.envfile automatically and mounts the project workspace for active local development inside the container usingbun dev. -
Stop the Container:
docker compose down
All routes are prefixed with /api. Below are the primary endpoints and routing modules:
POST /api/auth/— Verify active auth tokenPOST /api/auth/login— Login user (Zod validated; yields JWT HttpOnly cookie)POST /api/auth/register— Register a new user (Zod validated)POST /api/auth/logout— Logout user (Clears HttpOnly cookie)POST /api/auth/refresh-access-token— Rotate Access/Refresh tokens
- Management endpoints for user profiles and roles.
- Operations related to the cargo shipping and logistics services provided by the Logistics Management System.
- Configuration routes for setting brand attributes like logo, headers, footer details, and social links.
- Submitting messages or request-for-quote operations for shipping and logistics services.
- Fetching and saving page sections (e.g., Privacy Policy, Terms, About page custom blocks).
- Operations to fetch and update details of company executive leaders.
- Rate Limiting: To prevent API abuse, individual endpoints restrict traffic to
10 requests per minute per IPunder the current environment configuration. - CORS Handling: Cross-Origin Resource Sharing is configured to support requests from the environment-specified
clientUrlandhttp://localhost:5173(with support for credential transfer). - HTTP Security Headers: Powered by
helmetto mitigate clickjacking, XSS, and MIME-sniffing vulnerabilities. - Global Error Handling: Unhandled routes and throw exceptions are caught gracefully, delivering structured error responses.
Logs are automatically categorized:
- Development: Dev logs print directly to standard output with beautiful colors depending on the log severity.
- Production: Combined requests are printed via
morganto standard output. - Log Files: All exceptions, errors, warnings, and success events are recorded in
src/app/logs/app.logformatted with active ISO-8601 timestamps.