A robust Express.js backend template with enterprise-grade features, designed for scalable and maintainable Node.js applications.
- π· TypeScript - Write type-safe code with enhanced developer experience and fewer runtime errors
- π Authentication System - Secure JWT-based authentication with role-based access control (RBAC)
- π API Documentation - Automatic Swagger documentation generation for your API endpoints
- π§ Environment Configuration - Seamless switching between development, testing, and production environments
- π³ Docker Support - Optimized multi-stage Docker builds for both development and production
- βοΈ AWS Deployment - Ready-to-use CloudFormation template for AWS ECS Fargate deployment
- π§ͺ Testing - Comprehensive Jest setup for unit and integration testing
- π Logging - Structured logging with Winston for better debugging and monitoring
- π‘οΈ Error Handling - Robust centralized error handling middleware
- β Code Quality - Enforced code standards with ESLint and Prettier configuration
Before you begin, ensure you have the following installed:
- Node.js (v14 or higher)
- npm or yarn package manager
- MongoDB (local installation or MongoDB Atlas account)
- Docker (optional, for containerized development/deployment)
- Clone the repository:
git clone https://github.com/HanlunWang/express-backend-template.git
cd express-backend-template- Install dependencies:
npm install
# or with yarn
yarn install- Set up environment variables:
cp .env.example .env- Configure your environment:
Open the
.envfile in your favorite editor and update the configuration values:- Database connection string
- JWT secret key
- API port
- Logging levels
- Other application-specific settings
npm run dev
# or with yarn
yarn devnpm run build
npm start
# or with yarn
yarn build
yarn startThis project features a sophisticated multi-stage Docker build process that optimizes both development and production environments.
# Start development environment with hot-reloading
npm run docker:dev
# Start production environment
npm run docker:prod
# Build development Docker image
npm run docker:dev:build
# Build production Docker image
npm run docker:prod:build
# Stop all running containers
npm run docker:stop
# Clean up containers, networks, and volumes
npm run docker:cleanThe Docker setup ensures consistent environments across all stages of development:
-
π οΈ Development Environment:
- Uses nodemon for automatic server restarts
- Mounts source code as a volume for real-time code changes
- Includes development dependencies for better debugging
-
π Production Environment:
- Uses compiled JavaScript code for optimal performance
- Minimizes image size by excluding development dependencies
- Implements proper Node.js best practices for containers
The project includes a comprehensive docker-compose.yml with the following services:
- app-dev: Development environment with hot-reloading capability
- app-prod: Production-optimized environment
- mongo: MongoDB database service with persistent data storage
The application automatically generates comprehensive API documentation using Swagger UI.
Once the application is running, access the interactive API documentation at:
http://localhost:3000/api-docs
This documentation includes:
- Detailed endpoint descriptions
- Request/response schemas
- Authentication requirements
- Interactive API testing capability
.
βββ src/ # Source code directory
β βββ config/ # Configuration files and environment setup
β βββ controllers/ # Request handlers and business logic
β βββ middleware/ # Express middleware components
β βββ models/ # Mongoose data models
β βββ routes/ # API route definitions
β βββ utils/ # Utility functions and helpers
β βββ __tests__/ # Test files
β βββ index.ts # Application entry point
βββ aws/ # AWS deployment configuration files
βββ dist/ # Compiled JavaScript output
βββ logs/ # Application log files
βββ .env.example # Example environment variables template
βββ .eslintrc.js # ESLint configuration
βββ .prettierrc # Prettier code formatting rules
βββ docker-compose.yml # Docker Compose service definitions
βββ Dockerfile # Multi-stage Docker build configuration
βββ .dockerignore # Files excluded from Docker context
βββ jest.config.js # Jest testing framework configuration
βββ package.json # Project dependencies and scripts
βββ tsconfig.json # TypeScript compiler configuration
βββ README.md # Project documentation
The project includes a comprehensive testing setup using Jest:
# Run all tests
npm test
# Run tests with coverage report
npm test -- --coverage
# Run tests in watch mode during development
npm test -- --watchThe testing framework supports:
- Unit tests for individual functions and components
- Integration tests for API endpoints
- Mock services for external dependencies
- Code coverage reporting
For production deployment using Docker:
- Build the production image:
npm run docker:prod:build- Run the production container:
npm run docker:prodThis setup is ideal for deploying to any Docker-compatible hosting service, including:
- Digital Ocean
- Heroku
- Google Cloud Run
- Azure Container Instances
The project includes ready-to-use AWS CloudFormation templates for deploying to AWS ECS Fargate:
- Package the CloudFormation template:
aws cloudformation package \
--template-file aws/cloudformation.yml \
--s3-bucket your-deployment-bucket \
--output-template-file packaged-template.yml- Deploy the CloudFormation stack:
aws cloudformation deploy \
--template-file packaged-template.yml \
--stack-name express-backend \
--parameter-overrides \
Environment=prod \
MongoDBUri=your-mongodb-uri \
JwtSecret=your-jwt-secret \
--capabilities CAPABILITY_IAMThe AWS deployment includes:
- Auto-scaling configuration
- Load balancing
- Secure environment variable management
- CloudWatch logging integration
- IAM role configuration
This project is meticulously designed to ensure consistency between development and production environments:
-
π³ Docker Multi-stage Builds: The Dockerfile uses multi-stage builds to create optimized images for both development and production while maintaining consistency.
-
π Environment Variables: Environment variables are managed consistently across all environments using dotenv.
-
π Volume Mounting: In development, source code is mounted as a volume for hot-reloading without rebuilding the container.
-
π¦ Dependency Management: Dependencies are installed consistently in all environments with package.json and yarn.lock/package-lock.json.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Have questions or suggestions? Feel free to open an issue or reach out to the maintainers.
β Star this repository if you find it useful! β