Controller Layer Guide
Overview​
The Controller Layer handles HTTP requests and responses. This is the outermost layer in MangoJS onion architecture - it depends on the Service Layer but contains minimal logic.
Purpose: Handle HTTP requests, validate input, call services, format responses
Location: services/{service-name}/src/routes/v1/
Key Principle: Controllers are thin layers that orchestrate HTTP communication. Business logic belongs in services.
Documentation​
| Document | Description |
|---|---|
| Setup | Controller setup, decorators, service resolution |
| Type Safety | Using API types, Request/Response typing |
| Authorization | Auth decorators and protection patterns |
| Examples | Complete CRUD example, error handling, swagger |
Quick Start​
Step 1: Define API Types​
Controllers use API types from the shared types package. See Type Organization Guide.
Step 2: Create Controller​
import { Controller, Get, Post } from "@mangojs/core";
import { Request, Response } from "@mangojs/core";
import type * as PBTypes from "@theunionsquare/pulcherbook-types";
import { serviceNameContainer } from "../../../inversify.config.ts";
const shopService = serviceNameContainer.get<ShopService>(ShopService, {
autobind: true,
});
@Controller("/api/{short-service-name}/v1/shops")
export class ShopController {
@Get("/")
public async getShops(req: Request, res: Response): Promise<Response> {
// Implementation
}
}
Step 3: Register Controller​
File: src/routes/v1/index.ts
import { ShopController } from "./shops/shop.controller";
export const routes = [ShopController];
Related​
- Service Layer - Business logic layer
- Onion Architecture - Layer dependencies