The controller

Controller: Responsible for processing incoming requests and returning responses to the client.

Quick initialization

To create a controller using the CLI, run the following command:

nest g controller cats
Copy the code

Figure 1: Execute the command in the project SRC directory.

Figure 2: Execute commands in the project root directory.

As shown in Figure 1 and Figure 2, whenever you run a command in a project, the controller defaults to the SRC directory. The generated controller code is shown in Figure 3.

Figure 3: CLI commands generate controller code.

routing

Routing: Controls which controller receives which requests. A combination of the optional path prefix in the controller and any path string declared in the request method decorator.

Typically, a controller has multiple routes, and different routes can perform different operations.

Routing is independent of the handler name.

A decorator

Decorator: Associates classes with the required metadata and enables Nest to create routing mappings (binding requests to the appropriate controllers).

The sample

Create a basic Controller using the class and the @Controller() decorator.

/ * * *@description: Basic controller example *@update: the 2021-09-08 18:10:08 *@author: Ada.H
*/

// cats.controller.ts
import { Controller, Get } from '@nest/common';

// Set the route path prefix to cats
@Controller('cats')
export class CatsController {
  // Interface method get and interface path independent part query
  @Get('query')
  findAll(): string {
    return 'Query all cats'; }}/ / interface routing: http://localhost:3000/cats/query
Copy the code

Using path prefixes in the @Controller() decorator makes it easy to group a set of related routes and minimizes code redundancy (you don’t have to repeat that part of the path prefix for every route in every file).

The @get () HTTP request method decorator tells Nest to create a handler for a particular endpoint of the HTTP request.

Endpoint: corresponds to the HTTP request method and routing path.

GET /cats/query, request route map (a combination of the optional controller path prefix and any path string declared in the request method decorator).

FindAll () handler function. Nest uses two different operational responses:

type The characteristics of
Standard (recommended) Built-in methods.

Just return the value and the rest is doneNestResponsible: when returning aJavaScriptObject or array is automatically serialized toJSON; When you return aJavaScriptBasic types (e.g.string.number.boolean), the original value is sent.
Library specific It can be passed at the function signature@Res()Inject library specific response objects (e.g.Express), you can use the native response handler function exposed by the response object (e.gExpressWhen usingresponse.status(200).send()Build the response).

Note: If you use both the @res () and @next () methods on a handler (select a librar-specific option), you must place @res ({passthrough: True}) Passthrough can be set to true to mask the automatic route disabling rule in standard mode and meet special service requirements. Such as the effect of setting cookie/header separately by injecting the response object, but leaving the rest to the framework.