Express.js API Structure
Table of contents
Directory Structure
The Express.js API follows a modular architecture pattern:
Key Components
The API is organized with the following key directories:
- src/controllers: Request handlers
- src/models: MongoDB schema definitions
- src/routes: API route definitions
- src/middlewares: Authentication and validation middleware
- src/services: Business logic layer
- src/config: Configuration files
API Endpoints
The API provides the following main endpoints:
- /api/auth: Authentication endpoints (login, register)
- /api/contacts: Contact management endpoints
- /api/users: User management endpoints
Authentication
The API uses JWT (JSON Web Tokens) for authentication:
- User logs in with credentials
- Server validates credentials and issues a JWT
- Client includes JWT in subsequent request headers
- Server validates JWT before processing protected requests