Skip to content

TKAkhter/express-typescript-boilerplate-advance

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

86 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Advance Node.js Express TypeScript Boilerplate

This is a Node.js Express TypeScript boilerplate designed to follow a structured architecture similar to NestJS, making it highly scalable, maintainable, and modular.
It is built with best practices in mind, incorporating Prisma ORM, Swagger API documentation, Redis caching, and Winston logging.

πŸš€ Why Use This Boilerplate?

Unlike basic Express.js boilerplates that lack structure and scalability, this boilerplate offers:

βœ… Modular Architecture – Inspired by NestJS, separating concerns into controllers, services, and routes.
βœ… Pre-configured TypeScript – Ensuring type safety, better development experience, and maintainability.
βœ… Built-in Authentication – Includes JWT authentication out of the box.
βœ… Prisma ORM Integration – Provides an easy-to-use and scalable database management system.
βœ… Redis Caching – Optimized for performance with ioredis for caching.
βœ… Centralized Logging – Uses winston for logging with MongoDB and file-based storage.
βœ… API Documentation – Auto-generated API documentation using swagger-ui-express.
βœ… Security Best Practices – Pre-configured helmet, hpp, cors, and rate limiting for enhanced security.
βœ… Unit Testing Setup – Pre-configured with Jest & Supertest.
βœ… Production-Ready – Includes scripts for deployment and Docker support (if needed).

πŸ›  Features

  • Express.js - Minimal and fast web framework.
  • TypeScript - Static typing for better maintainability.
  • Prisma ORM - Database management with migrations and type safety.
  • Swagger - API documentation (swagger-ui-express).
  • Redis - Caching for improved performance (ioredis).
  • Winston - Logging to both files and MongoDB.
  • JWT Authentication - Secure authentication flow.
  • Security Middleware - helmet, hpp, cors, rate limiting, and more.
  • File Uploads - multer integration for handling file uploads.
  • Testing Framework - Uses Jest & Supertest.
  • ESLint & Prettier - Code linting and formatting.

πŸš€ Quick Start

Clone the Repository

git clone https://github.com/TKAkhter/advance-expressjs-boilerplate.git
cd advance-expressjs-boilerplate

Install Dependencies

npm install

Setup Environment Variables

Create a .env file and configure necessary variables:

DATABASE_URL=your_database_url
REDIS_URL=your_redis_url
JWT_SECRET=your_jwt_secret

Run the Development Server

npm run dev

Build and Run in Production

npm run build
npm start

Run Tests

npm test

Lint and Format Code

npm run lint
npm run prettier

Prisma Database Setup

npm run prisma:gen     # Generate Prisma client
npm run prisma:push    # Push schema to the database

πŸ“– Project Structure

This project follows a modular and scalable structure, similar to NestJS.

/src
β”œβ”€β”€ controllers/   # Handles HTTP requests and responses
β”œβ”€β”€ services/      # Business logic and database interactions
β”œβ”€β”€ middleware/    # Express middlewares for security, logging, etc.
β”œβ”€β”€ models/        # Database models (Prisma schemas)
β”œβ”€β”€ routes/        # API route definitions
β”œβ”€β”€ utils/         # Utility functions and helpers
β”œβ”€β”€ config/        # Configuration files (env, database, etc.)
β”œβ”€β”€ tests/         # Unit and integration tests
β”œβ”€β”€ server.ts      # Application entry point

πŸ”Ή Code Structure Explained

  • Controllers β†’ Handle incoming requests and return responses.
  • Services β†’ Contain the business logic and interact with the database via Prisma.
  • Middleware β†’ Includes authentication, logging, and security layers.
  • Routes β†’ Define API endpoints, connecting controllers to Express.
  • Models β†’ Represent the database schema using Prisma.
  • Utils β†’ Common utility functions like error handling, response formatting, etc.
  • Config β†’ Stores configuration variables (like env settings).

πŸ“œ API Documentation

Swagger documentation is auto-generated and available at:

http://localhost:3000/api-docs

πŸš€ GitHub Releases & Versioning

This project follows Semantic Versioning (vX.Y.Z) with automatic tags and releases on every PR merged into main.

πŸ”Ή Version Rules:

Change Type Keyword in PR Title/Body Example Version Change
Major Update #major v1.0.0 β†’ v2.0.0
Minor Update #minor v1.0.0 β†’ v1.1.0
Patch Update (Default, no keyword) v1.0.0 β†’ v1.0.1

Example PRs and Resulting Versions

PR with a Patch Version Bump (Default)

πŸ“Œ PR Title: Fixed authentication bug
πŸ“Œ PR Body: "Resolved a minor issue in login flow."
πŸ”Ή Result: v1.0.0 β†’ v1.0.1


PR with a Minor Version Bump

πŸ“Œ PR Title: Added new profile feature #minor
πŸ“Œ PR Body: "Implemented user profile page."
πŸ”Ή Result: v1.0.0 β†’ v1.1.0


PR with a Major Version Bump

πŸ“Œ PR Title: Revamped API structure #major
πŸ“Œ PR Body: "Breaking changes: Updated API structure."
πŸ”Ή Result: v1.0.0 β†’ v2.0.0


πŸ›  How Releases Work Automatically

When a PR is merged into main, GitHub Actions runs the release workflow.
It checks the PR title and body for #major or #minor.
Creates a new tag and GitHub release based on the versioning rule.

🎯 Steps to Enable Auto Releases

  1. Ensure the GitHub workflow file is in place at .github/workflows/release.yml.
  2. Create PRs with meaningful titles & descriptions.
  3. Merge the PR into main, and a new version tag & release will be created automatically.

πŸ— Deployment

Docker (Optional)

This project can be easily containerized using Docker. A Dockerfile can be created as follows:

FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
CMD ["npm", "start"]
EXPOSE 3000

Deploying to Production

Ensure to set the correct environment variables and use a process manager like pm2:

pm2 start dist/server.js --name "express-app"
pm2 save
pm2 startup

πŸ“œ License

This project is licensed under the MIT License.