NestJS Controller詳解:處理不同HTTP請求方法及路由參數

NestJS 是一個用於構建高效、可擴展的 Node.js 服務端應用程序的框架。Controller是 NestJS 中的重要組成部分,它負責處理傳入的 HTTP 請求並返回響應。本文將詳細介紹如何在 NestJS 中創建Controller,處理不同的 HTTP 請求方法(如 GET、POST、PUT、DELETE),以及如何使用路由參數和請求體。

Controller概述

Controller用於定義應用程序中的路由和處理傳入的請求。每個Controller負責處理一組相關的請求,通常與特定的資源(如用戶、文章等)相關聯。在 NestJS 中,Controller通過裝飾器和類方法來定義。

創建Controller

在 NestJS 中,Controller通過 @Controller() 裝飾器來定義。以下是一個簡單的Controller示例:

1
import { Controller, Get } from "@nestjs/common";
2
3
@Controller("users")
4
export class UsersController {
5
@Get()
6
findAll() {
7
return "This action returns all users";
8
}
9
}

在上述示例中,我們定義了一個 UsersController 類,並使用 @Controller('users') 裝飾器將其標記為一個Controller,同時將其路由前綴設置為 usersfindAll 方法通過 @Get() 裝飾器標記為處理 GET 請求。

處理不同的HTTP請求方法

NestJS Controller可以處理多種 HTTP 請求方法,包括 GET、POST、PUT、DELETE 等。以下是一個示例,展示了如何在Controller中處理這些請求方法:

1
import {
2
Controller,
3
Get,
4
Post,
5
Put,
6
Delete,
7
Param,
8
Body,
9
} from "@nestjs/common";
10
11
@Controller("users")
12
export class UsersController {
13
@Get()
14
findAll() {
15
return "This action returns all users";
16
}
17
18
@Get(":id")
19
findOne(@Param("id") id: string) {
20
return `This action returns user with id ${id}`;
21
}
22
23
@Post()
24
create(@Body() createUserDto: CreateUserDto) {
25
return "This action adds a new user";
26
}
27
28
@Put(":id")
29
update(@Param("id") id: string, @Body() updateUserDto: UpdateUserDto) {
30
return `This action updates user with id ${id}`;
31
}
32
33
@Delete(":id")
34
remove(@Param("id") id: string) {
35
return `This action removes user with id ${id}`;
36
}
37
}

在上述示例中,我們定義了四個方法來處理不同的 HTTP 請求方法:

  • findAll 處理 GET 請求,返回所有用戶。
  • findOne 處理帶有路由參數的 GET 請求,返回特定用戶。
  • create 處理 POST 請求,創建一個新用戶。
  • update 處理帶有路由參數的 PUT 請求,更新特定用戶。
  • remove 處理帶有路由參數的 DELETE 請求,刪除特定用戶。

使用路由參數

路由參數用於在 URL 中傳遞參數。在 NestJS 中,可以通過 @Param() 裝飾器來獲取路由參數。以下是一個示例:

1
import { Controller, Get, Param } from "@nestjs/common";
2
3
@Controller("users")
4
export class UsersController {
5
@Get(":id")
6
findOne(@Param("id") id: string) {
7
return `This action returns user with id ${id}`;
8
}
9
}

在上述示例中,findOne 方法通過 @Get(':id') 裝飾器定義了一個帶有路由參數 id 的 GET 請求。通過 @Param('id') 裝飾器,我們可以獲取路由參數 id 的值。

使用請求體

請求體用於在請求中傳遞數據,通常用於 POST 和 PUT 請求。在 NestJS 中,可以通過 @Body() 裝飾器來獲取請求體。以下是一個示例:

1
import { Controller, Post, Body } from "@nestjs/common";
2
3
class CreateUserDto {
4
readonly name: string;
5
readonly age: number;
6
}
7
8
@Controller("users")
9
export class UsersController {
10
@Post()
11
create(@Body() createUserDto: CreateUserDto) {
12
return `This action adds a new user with name ${createUserDto.name} and age ${createUserDto.age}`;
13
}
14
}

在上述示例中,create 方法通過 @Post() 裝飾器定義了一個 POST 請求。通過 @Body() 裝飾器,我們可以獲取請求體中的數據,並且使用了 CreateUserDto 類來進行類型約束。

參數詳解

  • @Controller():用於定義Controller,並指定路由前綴。
  • @Get()@Post()@Put()@Delete():用於定義處理不同 HTTP 請求方法的路由。
  • @Param():用於獲取路由參數。
  • @Body():用於獲取請求體。

DTO(數據傳輸對象)

DTO 是用於封裝請求數據的對象。在上述示例中,我們定義了一個 CreateUserDto 類來封裝創建用戶請求的數據:

1
class CreateUserDto {
2
readonly name: string;
3
readonly age: number;
4
}

路由參數和請求體類型約束

通過使用 TypeScript 的類型約束,我們可以確保路由參數和請求體的數據類型正確。以下是一個綜合示例,展示了如何使用路由參數和請求體:

1
import { Controller, Get, Post, Put, Param, Body } from "@nestjs/common";
2
3
class UpdateUserDto {
4
readonly name: string;
5
readonly age: number;
6
}
7
8
@Controller("users")
9
export class UsersController {
10
@Get(":id")
11
findOne(@Param("id") id: string) {
12
return `This action returns user with id ${id}`;
13
}
14
15
@Post()
16
create(@Body() createUserDto: CreateUserDto) {
17
return `This action adds a new user with name ${createUserDto.name} and age ${createUserDto.age}`;
18
}
19
20
@Put(":id")
21
update(@Param("id") id: string, @Body() updateUserDto: UpdateUserDto) {
22
return `This action updates user with id ${id}, new name ${updateUserDto.name}, new age ${updateUserDto.age}`;
23
}
24
}

總結

本文詳細介紹了 NestJS 中的Controller,講解了如何創建Controller來處理不同的 HTTP 請求方法(如 GET、POST、PUT、DELETE),以及如何使用路由參數和請求體。