控制器:负责处理传入的 请求 和向客户端返回 响应
路由:每一个URL都是由网站的服务器端程序来接收并进行处理,最终定向到相应的资源的机制。
通常,每个控制器有多个路由,不同的路由可以执行不同的操作。
路由装饰器NestJS采用了一种方式:使用装饰器。NestJS框架中定义了若干个专门用于路由处理相关的装饰器,通过它们,可以非常容易的将普通的class类装饰成一个个路由控制器。
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
}
@Controller("home")
//这样改写以后,本地访问的URL就变成了:
//http://localhost:3000/home
// 主路径为 home
@Controller("home")
// 1. 固定路径
// 可匹配到的访问路径:
// http://localhost:3000/home/greeting
@Get("greeting")
// 2. 通配符路径(通配符可以有 ?, +, * 三种)
// 可匹配到的访问路径:
// http://localhost:3000/home/say_hi
// http://localhost:3000/home/say_hello
// http://localhost:3000/home/say_good
// ...



