LoopBack 4 Todo Application Tutorial - Add a Controller

Controllers

In LoopBack 4, controllers handle the request-response lifecycle for your API.Each function on a controller can be addressed individually to handle anincoming request (like a POST request to /todos), to perform business logic,and to return a response.

Controller is a class that implements operations defined by application’s API.It implements an application’s business logic and acts as a bridge between theHTTP/REST API and domain/database models.

In this respect, controllers are the regions in which most of your businesslogic will live!

For more information about Controllers, seeControllers.

Create your controller

You can create a REST controller using the CLI as follows:

  1. lb4 controller
  2. ? Controller class name: todo
  3. Controller Todo will be created in src/controllers/todo.controller.ts
  4. ? What kind of controller would you like to generate? REST Controller with CRUD functions
  5. ? What is the name of the model to use with this CRUD repository? Todo
  6. ? What is the name of your CRUD repository? TodoRepository
  7. ? What is the name of ID property? id
  8. ? What is the type of your ID? number
  9. ? What is the base HTTP path name of the CRUD operations? /todos
  10. create src/controllers/todo.controller.ts
  11. update src/controllers/index.ts
  12. Controller Todo was created in src/controllers/

Let’s review the TodoController located insrc/controllers/todo.controller.ts. The @repository decorator will retrieveand inject an instance of the TodoRepository whenever an inbound request isbeing handled. The lifecycle of controller objects is per-request, which meansthat a new controller instance is created for each request. As a result, we wantto inject our TodoRepository since the creation of these instances is morecomplex and expensive than making new controller instances.

Note:You can customize the lifecycle of all bindings in LoopBack 4! Controllers can easily be made to use singleton lifecycles to minimize startup costs. For more information, see the Dependency injection section of our docs.

In this example, there are two new decorators to provide LoopBack with metadataabout the route, verb and the format of the incoming request body:

  • @post('/todos') creates metadata for @loopback/rest so that it canredirect requests to this function when the path and verb match.
  • @requestBody() associates the OpenAPI schema for a Todo with the body of therequest so that LoopBack can validate the format of an incoming request.Some additional things to note about this example:

  • Routes like @get('/todos/{id}') can be paired with the @param.pathdecorators to inject those values at request time into the handler function.

  • LoopBack’s @param decorator also contains a namespace full of other“subdecorators” like @param.path, @param.query, and @param.header thatallow specification of metadata for those parts of a REST request.
  • LoopBack’s @param.path and @param.query also provide subdecorators forspecifying the type of certain value primitives, such as@param.path.number('id').Now that we’ve wired up the controller, our last step is to tie it all into theApplication!

Navigation

Previous step: Add a repository

Final step: Putting it all together