A production-ready Express.js boilerplate with TypeScript, TypeDI, routing-controllers, class-validator, OpenAPI documentation, and auto-generated client SDK using Kubb.
- TypeScript: Full type safety with strict mode enabled
- Dependency Injection: Clean architecture with TypeDI
- Declarative Routing: Type-safe routing with routing-controllers decorators
- Validation: Automatic request validation with class-validator
- OpenAPI/Swagger: Auto-generated API documentation from code
- Client SDK Generation: Type-safe client SDK with Kubb (TypeScript types, Zod schemas, React Query hooks)
- Error Handling: Centralized error handling middleware
- CORS: Configured and ready to use
- Hot Reload: Development server with nodemon
- Code Quality: ESLint and Prettier configured
- Real-world Example: Complete CRUD operations for Users and Products
- Node.js >= 20.0.0
- npm or yarn or pnpm
- Clone the repository:
git clone [<repository-url>](https://github.com/fishuke/express-typescript-boilerplate.git)
cd express-typescript-boilerplate- Install dependencies:
npm install- Copy environment variables:
cp .env.example .env- Start development server:
npm run devThe API will be available at:
- API: http://localhost:3000/api
- API Documentation: http://localhost:3000/api-docs
- OpenAPI Spec: http://localhost:3000/api/openapi.json
- Health Check: http://localhost:3000/health
express-typescript-openapi-boilerplate/
βββ src/
β βββ controllers/ # API controllers with routing-controllers
β β βββ UserController.ts
β β βββ ProductController.ts
β βββ services/ # Business logic with TypeDI
β β βββ UserService.ts
β β βββ ProductService.ts
β βββ models/ # Data models and interfaces
β β βββ User.ts
β β βββ Product.ts
β βββ dto/ # Data Transfer Objects with validation
β β βββ CreateUserDto.ts
β β βββ UpdateUserDto.ts
β β βββ CreateProductDto.ts
β β βββ UpdateProductDto.ts
β βββ middleware/ # Express middleware
β β βββ errorHandler.ts
β βββ utils/ # Utility functions
β β βββ generateOpenApiSpec.ts
β βββ app.ts # Application entry point
βββ client-sdk/ # Auto-generated client SDK (via Kubb)
β βββ types/ # TypeScript types
β βββ zod/ # Zod validation schemas
β βββ client/ # HTTP client functions
β βββ hooks/ # React Query hooks
βββ openapi/
β βββ spec.json # Generated OpenAPI specification
βββ kubb.config.ts # Kubb configuration
βββ tsconfig.json # TypeScript configuration
βββ nodemon.json # Nodemon configuration
βββ .eslintrc.json # ESLint configuration
βββ .prettierrc # Prettier configuration
βββ package.json
GET /api/users- Get all users (with optional role filter)GET /api/users/active- Get active usersGET /api/users/:id- Get user by IDPOST /api/users- Create a new userPUT /api/users/:id- Update userDELETE /api/users/:id- Delete user
GET /api/products- Get all products (with optional category/search filters)GET /api/products/available- Get available productsGET /api/products/:id- Get product by IDPOST /api/products- Create a new productPUT /api/products/:id- Update productPATCH /api/products/:id/stock- Update product stockDELETE /api/products/:id- Delete product
# Development
npm run dev # Start dev server with hot reload
# Build
npm run build # Compile TypeScript to JavaScript
# Production
npm start # Start production server
# Code Generation
npm run generate:openapi # Generate OpenAPI specification
npm run generate:client # Generate client SDK with Kubb
npm run generate # Generate both OpenAPI spec and client SDK
# Code Quality
npm run lint # Run ESLint
npm run format # Format code with Prettier- Express.js - Fast, unopinionated web framework
- TypeScript - Type safety and better developer experience
- TypeDI - Dependency injection container
- routing-controllers - Declarative routing with decorators
- class-validator - Decorator-based validation
- class-transformer - Object transformation
- routing-controllers-openapi - OpenAPI spec generation
- Swagger UI Express - Interactive API documentation
- OpenAPI 3.0 - API specification standard
- Kubb - OpenAPI to TypeScript code generator
- TypeScript types generation
- Zod schemas for runtime validation
- HTTP client functions
- TanStack Query (React Query) hooks
// Creating a controller
@JsonController('/users')
@Service()
export class UserController {
constructor(private userService: UserService) {}
@Get('/')
@OpenAPI({ summary: 'Get all users' })
async getAll(): Promise<User[]> {
return this.userService.findAll();
}
@Post('/')
@HttpCode(201)
async create(@Body() dto: CreateUserDto): Promise<User> {
return this.userService.create(dto);
}
}After running npm run generate, you can use the generated client:
// Using the generated client in your frontend
import { getUsers, createUser } from './client-sdk/client';
// Fetch all users
const users = await getUsers();
// Create a new user
const newUser = await createUser({
email: 'user@example.com',
name: 'John Doe',
role: 'user'
});import { useGetUsers, useCreateUser } from './client-sdk/hooks';
function UsersComponent() {
// Auto-generated React Query hook with TypeScript types
const { data: users, isLoading } = useGetUsers();
const createMutation = useCreateUser();
const handleCreateUser = () => {
createMutation.mutate({
email: 'new@example.com',
name: 'New User',
role: 'user'
});
};
return (
<div>
{isLoading ? 'Loading...' : users?.map(user => (
<div key={user.id}>{user.name}</div>
))}
<button onClick={handleCreateUser}>Create User</button>
</div>
);
}export class CreateUserDto {
@IsEmail({}, { message: 'Invalid email address' })
@IsNotEmpty({ message: 'Email is required' })
email: string;
@IsString({ message: 'Name must be a string' })
@MinLength(2, { message: 'Name must be at least 2 characters long' })
name: string;
@IsEnum(UserRole, { message: 'Invalid role' })
role: UserRole;
}npm run buildnpm startFROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY dist ./dist
EXPOSE 3000
CMD ["node", "dist/app.js"]This boilerplate is ready for testing. You can add:
- Unit tests with Jest
- Integration tests with Supertest
- E2E tests with your preferred framework
- TypeScript
- Express.js
- TypeDI
- routing-controllers
- class-validator
- Kubb
- OpenAPI Specification
- TanStack Query
Contributions, issues, and feature requests are welcome!
This project is MIT licensed.
Your Name
- GitHub: @yourusername
Give a βοΈ if this project helped you!
Note: This is a boilerplate/starter project. In a real-world application, you would typically:
- Add a database (PostgreSQL, MongoDB, etc.)
- Implement authentication & authorization (JWT, OAuth)
- Add logging (Winston, Pino)
- Add testing (Jest, Supertest)
- Implement rate limiting
- Add request/response caching
- Set up CI/CD pipelines
- Add monitoring and observability