A well-structured Express.js API template built with TypeScript, featuring a clean architecture for building scalable backend applications with proper separation of concerns.
express-typescript-template/
├── src/
│ ├── client/ # External API clients
│ │ ├── jsonplaceholder.client.ts # JSONPlaceholder API client
│ │ └── jsonplaceholder.example.ts # Example usage of the client
│ ├── config/ # Application configuration
│ │ ├── index.ts # Configuration exports
│ │ ├── typeorm.config.ts # TypeORM database configuration
│ │ └── logger.ts # Logging configuration
│ ├── controllers/ # Request handlers
│ │ └── task.controller.ts # Task controller with input validation
│ ├── entities/ # TypeORM entities
│ │ └── Task.ts # Task entity definition
│ ├── middlewares/ # Express middlewares
│ │ ├── auth.middleware.ts # Authentication middleware
│ │ └── error-handler.middleware.ts # Global error handler
│ ├── routes/ # API routes
│ │ └── task.routes.ts # Task routes with Swagger docs
│ ├── schemas/ # Validation schemas
│ │ ├── task.schema.ts # Zod validation schemas for tasks
│ │ └── jsonplaceholder.schema.ts # Zod schemas for JSONPlaceholder API
│ ├── services/ # Business logic
│ │ └── task.service.ts # Task service with DTOs for API contracts
│ ├── types/ # Type definitions
│ │ ├── task.types.ts # Task-related type definitions and DTOs
│ │ └── jsonplaceholder.types.ts # JSONPlaceholder API type definitions
│ ├── utils/ # Utility functions
│ │ └── errors.ts # Custom error classes
│ └── migrations/ # Database migrations
├── .env.example # Example environment variables
├── .gitignore # Git ignore configuration
├── package.json # Project dependencies
├── tsconfig.json # TypeScript configuration
└── README.md # Project documentation
This template follows a traditional layered architecture where:
-
Controllers: Handle HTTP requests and responses
- Input validation using Zod schemas
- Proper error handling
- Calling appropriate services
-
Services: Contain business logic and data operations
- Use DTOs for input/output to hide entity details
- Handle database operations through repositories
- Implement business rules
-
Entities: Define database models
- TypeORM entity definitions
- Field validations and relationships
-
Schemas: Define validation rules for input data
- Zod schemas for request validation
- Type inference for TypeScript safety
-
Middlewares: Common request processing logic
- Authentication
- Error handling
- Request validation
-
Routes: Define API endpoints with Swagger documentation
-
Types: Type definitions and DTOs
- Input/output data transfer objects
- Type safety throughout the application
-
Utils: Shared utility functions
- Custom error classes
- Logging utilities
- Node.js (v14+)
- npm or yarn
-
Clone the repository:
git clone <repository-url> cd express-typescript-template
-
Install dependencies:
npm install # or yarn
-
Create environment file:
cp .env.example .env
-
Start development server:
npm run dev # or yarn dev
The server will be running at http://localhost:3000 with API documentation available at http://localhost:3000/api-docs.
- TypeScript: Type-safe code with proper interfaces and types
- Traditional Layered Architecture: Clear separation of concerns
- Input Validation: Using Zod schemas for request validation
- Data Transfer Objects: Proper separation between API contracts and database entities
- TypeORM: Database ORM with SQLite support
- Error Handling: Centralized error handling
- Logging: Structured logging
- API Documentation: Auto-generated with Swagger
- Authentication: JWT-based authentication (template ready)
- Environment Configuration: Using dotenv
- External API Client: JSONPlaceholder API client implementation
To add a new feature:
- Create a new entity in
src/entities/
- Create validation schemas in
src/schemas/
- Define types and DTOs in
src/types/
- Implement the service in
src/services/
- Create a controller in
src/controllers/
- Define routes in
src/routes/
- Register the routes in the main application
The template includes a client for the JSONPlaceholder API (https://jsonplaceholder.typicode.com/):
-
Import the client in your service or controller:
import { JsonPlaceholderClient } from '../client/jsonplaceholder.client';
-
Create an instance and use the methods:
const client = new JsonPlaceholderClient(); const posts = await client.getPosts(); const user = await client.getUserById(1);
-
Available methods include:
getPosts()
- Get all postsgetPostById(id)
- Get a post by IDcreatePost(post)
- Create a new postupdatePost(id, post)
- Update a postdeletePost(id)
- Delete a postgetCommentsByPostId(postId)
- Get comments for a postgetUsers()
- Get all usersgetUserById(id)
- Get a user by IDgetTodosByUserId(userId)
- Get todos for a usergetAlbumsByUserId(userId)
- Get albums for a usergetPhotosByAlbumId(albumId)
- Get photos for an album
-
See
src/client/jsonplaceholder.example.ts
for usage examples
API documentation is automatically generated using Swagger. Access it at /api-docs
when the server is running.
The application uses a centralized error handling approach:
- Custom error classes in
src/utils/errors.ts
- Global error handler middleware in
src/middlewares/error-handler.middleware.ts
The template includes JWT-based authentication:
- Middleware in
src/middlewares/auth.middleware.ts
- Apply to routes using the
authenticate
middleware
This project is licensed under the MIT License - see the LICENSE file for details.