The Express.js backend API design follows a clean separation of concerns. Routes handle URL patterns and HTTP methods. Controllers handle business logic and delegate to service and repository layers. Middleware handles cross-cutting concerns like authentication, validation, and error handling. The route structure for a task management system includes endpoints for listing tasks, creating tasks, updating task status, and deleting tasks.
Authentication is handled through JWT tokens. The authentication middleware verifies tokens on protected routes, extracts the user ID from the token payload, and loads the corresponding user from the database. The middleware returns 401 Unauthorized responses when tokens are missing, invalid, or expired.
Input validation is performed using express-validator or a custom validation middleware. Validation rules check request body fields for required fields, data types, and pattern matching. Invalid input returns 400 Bad Request responses with detailed error messages.