Express.js API Structure

Table of contents

  1. Directory Structure
  2. Key Components
  3. API Endpoints
  4. Authentication

Directory Structure

The Express.js API follows a modular architecture pattern:

api

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:

  1. /api/auth: Authentication endpoints (login, register)
  2. /api/contacts: Contact management endpoints
  3. /api/users: User management endpoints

Authentication

The API uses JWT (JSON Web Tokens) for authentication:

  1. User logs in with credentials
  2. Server validates credentials and issues a JWT
  3. Client includes JWT in subsequent request headers
  4. Server validates JWT before processing protected requests