Skip to content

Controllers

Anshuman Chhapolia edited this page Mar 23, 2024 · 1 revision

Controllers

Controllers are the part of the application that handle the incoming HTTP request. Currently the controllers are based on the express RouteHandlers. The controllers are responsible for handling the incoming request, processing the request body and parameters, validating the request, and sending the response back to the client.

The controllers once the request is ready to be processed (parsed and validated) will call the service layer to process the request. This seperation of concerns allows for the controllers to be easily tested and the service layer to be easily reused.

Here is an example of a simple controller:

class ExampleController extends Controller {
  path = '/example';
  controllers = [];
  middleware = [];
  constructor(app: Application ,public exampleService: ExampleService) {
    super(app);

    this.routes.add({
      path: '/example',
      method: Methods.GET,
      localMiddleware: [],
      handler: this.getExample.bind(this),
    });
  }

  private getExample = async (req: Request, res: Response, next: NextFunction) => {
    try {
      const example = await this.exampleService.getExample();
      res.status(200).json(example);
    } catch (error) {
      next(error);
    }
  }
}

Path is the base path for the controller. The controllers array consist of controller objects to form the route tree and seperate the routes into different files.

The middleware array is an array of middleware functions that will be applied to all routes in the controller.

The constructor takes the express application and the service layer as arguments. The constructor calls the super constructor with the Application instance.

The routes.add method is used to add a route to the controller. The route object consists of the path, method, localMiddleware, and handler. The path is the path relative to the controller path. The method is the HTTP method. The localMiddleware is an array of middleware functions that will be applied to the route. The handler is the function that will be called when the route is hit. The handler is bound to the controller instance so that the controller instance can be accessed in the handler.

The handler is an async function that takes the express request, response, and NextFunction as arguments. The handler calls the service layer to process the request and sends the response back to the client.

The controller is then added to the application in the Application class.

app.addController(new ExampleController(app, new ExampleService()));

ServiceController

The ServiceController is a special controller that is used to expose the predefined CRUD routes with documentation for them. The ServiceController requires using an instance of the Service class. The ServiceController will automatically generate the routes for the CRUD operations and will generate the documentation for the routes.

Here is an example of a ServiceController:

export class AddressController extends ServiceController<Address> {
  path: string = "/address";
  protected controllers: Controller[] = [];
  protected mw: RequestHandler[] = [];

  service: AddressService;

  constructor(app: Application, addressService: AddressService) {
    super(app, Address, addressService);

    this.service = addressService;

    this.loadDocumentation();
  }
}

The ServiceController is a subclass of the Controller class. The ServiceController requires the Entity class to be passed in as a generic type. The ServiceController also requires the Entity and Service class to be passed in as an argument to the constructor.