A robust Node.js backend application built with TypeScript, following Clean Architecture principles and SOLID design patterns.
- Architecture Overview
- Technology Stack
- Project Structure
- Getting Started
- Configuration
- API Documentation
- Testing
- Error Handling
- Database
- Authentication
- Deployment
This project implements Clean Architecture principles, separating concerns into distinct layers:
- Contains business logic and domain entities
- Defines interfaces and use cases
- Independent of external frameworks and tools
- Houses DTOs (Data Transfer Objects) and business rules
- Implements use cases defined in the core layer
- Orchestrates data flow between layers
- Contains application-specific business rules
- Manages transactions and coordinates responses
- Implements interfaces defined in core layer
- Contains database implementations
- Handles external services and frameworks
- Manages technical concerns (caching, logging, etc.)
- Express.js controllers and routes
- Request/Response handling
- Input validation
- Authentication middleware
- Runtime: Node.js
- Language: TypeScript
- Framework: Express.js
- Database: PostgreSQL
- ORM: None- Custom SQL implementation
- Dependency Injection: InversifyJS
- Authentication: JWT (JSON Web Tokens)
- Testing: Jest
- Validation: class-validator and custom validation
- File Upload: Multer
- Security: bcrypt, CORS
src/
├── Core/
│ ├── Application/
│ │ ├── DTOs/
│ │ ├── Entities/
│ │ ├── Enums/
│ │ ├── Error/
│ │ ├── Interface/
│ │ ├── Response/
│ │ └── UseCases/
│ ├── DIContainer.ts
│ ├── Services/
│ └── Types/
├── Infrastructure/
│ ├── Database/
│ ├── Repository/
│ │ ├── MongoDB/
│ │ └── SQL/
│ └── Services/
├── Middleware/
└── Controllers/
- Node.js (v14 or higher)
- PostgreSQL (v12 or higher)
- npm or yarn
- Clone the repository:
git clone <repository-url>
cd name-of-project
- Install dependencies:
npm install
- Create environment file:
cp .env.example .env
- Configure environment variables in
.env
:
PORT=3000
NODE_ENV=development
# Database Configuration
DB_USER=postgres
DB_PASSWORD=your_password
DB_HOST=localhost
DB_PORT=5432
DB_NAME=your_database
DB_SSL=false
DB_POOL_MAX=10
DB_IDLE_TIMEOUT=30000
DB_CONNECTION_TIMEOUT=2000
# JWT Configuration
JWT_ACCESS_SECRET=your_access_token_secret
JWT_REFRESH_SECRET=your_refresh_token_secret
JWT_ACCESS_EXPIRATION=15m
JWT_REFRESH_EXPIRATION=7d
- Start the development server:
npm run dev
The system uses PostgreSQL with connection pooling. Configure the following in .env
:
DB_USER
: Database usernameDB_PASSWORD
: Database passwordDB_HOST
: Database hostDB_PORT
: Database portDB_NAME
: Database nameDB_SSL
: Enable/disable SSLDB_POOL_MAX
: Maximum pool connectionsDB_IDLE_TIMEOUT
: Connection idle timeoutDB_CONNECTION_TIMEOUT
: Connection timeout
JWT settings for authentication:
JWT_ACCESS_SECRET
: Secret for access tokensJWT_REFRESH_SECRET
: Secret for refresh tokensJWT_ACCESS_EXPIRATION
: Access token expirationJWT_REFRESH_EXPIRATION
: Refresh token expiration
POST /api/v1/auth/register
Content-Type: application/json
{
"first_name": "string",
"last_name": "string",
"email": "string",
"password": "string",
"roles": ["string"]
}
POST /api/v1/auth/login
Content-Type: application/json
{
"email": "string",
"password": "string"
}
GET /api/v1/auth/profile
Authorization: Bearer <token>
PUT /api/v1/auth/profile
Authorization: Bearer <token>
Content-Type: application/json
{
"first_name": "string",
"last_name": "string",
"email": "string"
}
The system implements a comprehensive error handling system:
AppError
: Base error classValidationError
: Input validation errorsAuthenticationError
: Authentication failuresAuthorizationError
: Permission issuesNotFoundError
: Resource not foundConflictError
: Data conflictsDatabaseError
: Database operation failures
The system implements a robust transaction management system:
- Connection pooling for optimal performance
- Transaction isolation levels
- Automatic rollback on errors
- Connection lifecycle management
Each entity has its own repository implementing:
- CRUD operations
- Custom queries
- Transaction support
- Error handling
The system uses a JWT-based authentication system:
- Access tokens for API authentication
- Refresh tokens for token renewal
- Role-based authorization
- Token blacklisting
- Secure password hashing
- Password hashing with bcrypt
- JWT token encryption
- CORS protection
- Rate limiting
- Input validation
- SQL injection protection
Run tests using:
npm run test
- Unit tests for business logic
- Integration tests for APIs
- Repository tests
- Authentication tests
- Error handling tests
npm run dev
: Start development servernpm run build
: Build production versionnpm start
: Start production servernpm test
: Run testsnpm run lint
: Run linternpm run lint:fix
: Fix linting issues
- Follow TypeScript best practices
- Use dependency injection
- Write unit tests for business logic
- Document API endpoints
- Handle errors appropriately
- Use proper typing
- Follow clean code principles
- Build the application:
npm run build
- Set production environment variables
- Start the server:
npm start
- Fork the repository
- Create a feature branch
- Commit changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.