A robust, production-ready Express.js API template built with TypeScript, featuring comprehensive testing, documentation, and modern development practices.
- TypeScript - Full type safety and modern JavaScript features
- Express.js - Fast, unopinionated web framework
- Central Entry Point - Robust application architecture with dependency injection
- Prisma - Modern database toolkit with type-safe queries
- Jest - Comprehensive testing suite with unit and integration tests
- Swagger/OpenAPI - Interactive API documentation
- Winston - Structured logging
- Rate Limiting - Built-in request rate limiting
- Security - Helmet.js for security headers
- Validation - Class-validator for request validation
- Error Handling - Custom error classes and centralized error handling
- Environment Configuration - Centralized config management
- Docker Support - Ready for containerization
- Code Quality - Prettier, ESLint, and TypeScript strict mode
src/
βββ app/ # Central entry point architecture
β βββ application.ts # Main application class
β βββ service-registry.ts # Service management
βββ bootstrap/ # Configuration management
β βββ app.ts # Express app setup
β βββ config.ts # Main application class
β βββ errors.ts # Custom error classes
β βββ modules.ts # Main application class
β βββ seeders.ts # Main application class
β βββ services.ts # Main application class
βββ features/ # Feature-based modules
β βββ user/ # User feature module
β βββ user.controller.ts
β βββ user.service.ts
β βββ user.repo.ts
β βββ user.dto.ts
β βββ user.entity.ts
βββ middlewares/ # Express middlewares
βββ services/ # Shared services
βββ utils/ # Utility functions
βββ server.ts # Central entry point
βββ __tests__/ # Test files
β βββ unit/ # Unit tests
β βββ integration/ # Integration tests
β βββ prisma.config.ts # Test configuration
- Node.js (v18 or higher)
- PostgreSQL (v12 or higher)
- npm or yarn
npx degit https://github.com/ebukaodini/express-api-template.git my-api
cd my-apipnpm installCreate a .env file in the root directory:
cp .env.example .envUpdate the environment variables:
# Server Configuration
PORT=3000
NODE_ENV=development
# Database Configuration
DATABASE_URL="postgresql://username:password@localhost:5432/sample_api"
# CORS Configuration
CORS_ORIGIN="http://localhost:3000,http://localhost:3001"
CORS_CREDENTIALS=true
# Rate Limiting
RATE_LIMIT_WINDOW_MS=900000
RATE_LIMIT_MAX=100
# JWT Configuration
JWT_SECRET="your-super-secret-jwt-key-change-this-in-production"
JWT_EXPIRES_IN="24h"
# Logging
LOG_LEVEL="debug"# Generate Prisma client
pnpm prisma:generate
# Run database migrations
pnpm prisma:migrate
# Seed the database (optional)
pnpm seed:devTraditional way:
pnpm devThe API will be available at http://localhost:3000
Once the server is running, you can access the interactive API documentation at:
- Swagger UI:
http://localhost:3000/api-docs - Health Check:
http://localhost:3000/health
pnpm testpnpm run test:watchnpm run test:coveragenpm run test:integration| Script | Description |
|---|---|
npm run dev |
Start development server with hot reload |
npm run build |
Build the project for production |
npm start |
Start production server |
npm test |
Run all tests |
npm run test:watch |
Run tests in watch mode |
npm run test:coverage |
Run tests with coverage report |
npm run test:integration |
Run integration tests only |
npm run test:central |
Run central entry point tests |
npm run example |
Run central entry point examples |
npm run start:central |
Start with central entry point |
npm run dev:central |
Development with central entry point |
npm run lint |
Format code with Prettier |
npm run lint:check |
Check code formatting |
npm run prisma:migrate |
Run database migrations |
npm run prisma:generate |
Generate Prisma client |
npm run prisma:seed |
Seed the database |
npm run docs |
Display API documentation info |
This template implements a robust central entry point architecture that provides:
- Application Class - Main orchestrator for application lifecycle
- Dependency Injection - Container-based service management
- Service Registry - Centralized service lifecycle management
- Service Factory - Factory pattern for service creation
- Health Monitoring - Comprehensive service health checks
The project follows a feature-based architecture where each feature is self-contained with its own:
- Controller - Handles HTTP requests and responses
- Service - Contains business logic
- Repository - Handles data access
- DTO - Data transfer objects for validation
- Entity - Domain models
- Routes - API route definitions
- Custom error classes for different error types
- Centralized error handling middleware
- Structured error responses
- Proper HTTP status codes
- Winston logger with multiple transports
- Structured logging with timestamps
- Different log levels for different environments
- Request/response logging
- Helmet.js for security headers
- CORS configuration
- Rate limiting
- Input validation
- SQL injection protection (via Prisma)
# Build the Docker image
docker build -t my-api .
# Run the container
docker run -p 3000:3000 my-apiMake sure to set the following environment variables in production:
NODE_ENV=productionDATABASE_URL- Your production database URLJWT_SECRET- A strong, random secret keyCORS_ORIGIN- Your production frontend URLLOG_LEVEL=info
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for your changes
- Ensure all tests pass
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with scaffold.js
- Inspired by modern Node.js best practices
- Community contributions and feedback