使用cli 创建控制器
$ nest g controller students
$ nest g service students
$ nest g module students
//也可以直接使用下边的指令生成资源
$ nest g res students --no-spec //--no-spec是不生成测试文件
执行完后目录结构如下
src
|- app.controller.spec.ts
|- app.controller.ts
|- app.module.ts
|- app.service.ts
|- main.ts
|- students/
|- students.controller.spec.ts //单元测试
|- students.controller.ts
|- students.module.ts
|- students.service.spec.ts//单元测试
|- students.service.ts
//students.service.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class StudentsService {
ImStudent() {
return 'Im student';
}
}
// students.controller.ts
import { Controller, Get } from '@nestjs/common';
import { StudentsService } from './students.service';
@Controller('students')
export class StudentsController {
//private readonly是TypeScript中的访问修饰符,用于指定该属性只能在类内部访问,并且不能被修改
constructor(private readonly studentsService: StudentsService) {}
@Get('who-are-you')
//方法名和路由无关
whoAreYou() {
return this.studentsService.ImStudent();
}
}
浏览器输入 localhost:3000/students/wo-arr-you
GET POST请求
import { Controller, Get,Post } from '@nestjs/common';
import { CatsService } from './cats.service';
@Controller('cats')
export class CatsController {
constructor(private readonly catsService: CatsService) {}
@Get('hehe')
findAllGet(): string {
return this.catsService.GetAll();
}
@Post('xixi')
findAllPost(): string {
return this.catsService.GetAll();
}
}
Get参数
//students.controller.ts
import { Controller, Get,Post,Query } from '@nestjs/common';
import { CatsService } from './cats.service';
@Controller('cats')
export class CatsController {
constructor(private readonly catsService: CatsService) {}
@Get('hehe')
findAllGet(@Query('name') name:string): string {
return this.catsService.GetAll(name);
}
}
//students.service.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class CatsService {
GetAll(name?:string){
return "get all-"+name
}
}
Post 参数
post比较特殊需要将文本类型转换为变量类型
//str/dtos/cats.dto.ts
export class CatsDto {
name: string;
}
//students.controller.ts
import { Body, Controller, Get,Post,Query } from '@nestjs/common';
import { CatsService } from './cats.service';
import { CatsDto } from '../dtos/cats.dto';
@Controller('cats')
export class CatsController {
constructor(private readonly catsService: CatsService) {}
@Post('xixi')
findAllPost(@Body()cats:CatsDto): string {
return this.catsService.GetAll(cats.name);
}
}
参数的限制与转换
get请求 管道将string类型转换为number类型
//students.service.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class CatsService {
GetAll(id:number){
const allCats={
1:'小明'
}
return allCats[id]??'not fond'
}
}
//students.controller.ts
import { Body, Controller, Get,Post,Query,ParseIntPipe } from '@nestjs/common';
import { CatsService } from './cats.service';
import { CatsDto } from '../dtos/cats.dto';
@Controller('cats')
export class CatsController {
constructor(private readonly catsService: CatsService) {}
@Get('hehe')
findAllGet(@Query('id',ParseIntPipe) id:number): string {
return this.catsService.GetAll(id);
}
}