Controller, @Get(), @Post()

  1. Generate a controller by running the Nest CLI

    Command: nest generate controller, which can be abbreviated as nest g co. Then enter the name of the controller. (If you don’t want to generate test files, you should type nest g co --no-spec )

    After running, nest will help us create a folder containing controller and spec files. At this time, the app.module.ts file will automatically introduce this controller to the controllers array.

    If you want to create a controller in a subfolder of src, the command can be rewritten as nest generate controller modules/abc. If you want to see the simulated output, you can use nest generate controller modules/abc --dry-run. He doesn’t actually create the file.


  2. URL access and controller matching mechanism

    Nest uses @Controller('route') to match controller and url.

    1
    2
    @Controller('coffees') //It can be accessed when accessing http://localhost:3000/coffees with GET.
    export class CoffeesController {}

    When controller is empty, it will return 404 and error message. Also note that methods like Controller and Get/Post are imported from @nestjs/common.


  3. Build http handler, Get

    First introduce Methods such as Get from @nestjs/common. The corresponding methods for Get are findAll() and findOne.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    import { Controller, Get } from '@nestjs/common';

    @Controller('coffees')
    export class CoffeesController {
    @Get()
    findAll(){
    return 'This action returns all coffees';
    }
    }

  4. Nested Paths

    In a class decorated with @Controller('primary path'), secondary paths can be nested.

    1
    2
    3
    4
    5
    6
    7
    @Controller('coffees')
    export class CoffeesController {
    @Get('flavors') //Put the secondary route here.
    findAll(){
    return 'This action returns all coffees';
    }
    }

    At this time, visit localhost:3000/coffees/flavors to access the content.


    Use routing parameters (dynamic path)

    You can use route parameters by writing (':id') in the decorator, and import Param from @nestjs/common. The @Param() decorator allows us to take all incoming request parameters and use them in the body of the method.

    1. When nothing is passed inside the @Param() decorator, it receives all request parameters, let’s access params.id from the object.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      import { Controller, Get, Param } from '@nestjs/common';

      @Controller('coffees')
      export class CoffeesController {
      @Get(':id') //Put the secondary route here.
      findOne(@Param() params){
      return `This action returns #${params.id} coffees`;
      }
      }

    2. When the @Param() decorator passes in a parameter, first declare the parameter type, and then use the parameter directly.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      import { Controller, Get, Param } from '@nestjs/common';

      @Controller('coffees')
      export class CoffeesController {
      @Get(':id')
      findOne(@Param('id') id: string){ //The parameter type is declared here.
      return `This action returns #${id} coffees`;
      }
      }


  1. Post and @Body decorators

    The @Body decorator is used to get all or specific parts of request.body. Note that both Post and Body are imported from @nestjs/common. The corresponding method of Post is create .

    1
    2
    3
    4
    5
    6
    7
    8
    9
    import { Controller, Get, Param, Post, Body } from '@nestjs/common';

    @Controller('coffees')
    export class CoffeesController {
    @Post()
    create(@Body() body){
    return body;
    }
    }

    As with the @Param decorator, it is possible not to access the body as a whole, but a specific part.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    import { Controller, Get, Param, Post, Body } from '@nestjs/common';

    @Controller('coffees')
    export class CoffeesController {
    @Post()
    create(@Body('name') body){ //Incoming parameters
    return body;
    }
    }

    The problem with this way is that if only certain properties are accessed, other properties will not be validated.


Share