1、创建子模块
使用nest cli 脚手架命令,其中new/n代表新建项目,generate|g代表生成子模块或者其他,一般用generate|g来生成子模块。
在命令行运行nest g module ‘模块名称’ 创建帖子模块
nest g module posts
在命令行运行nest g controller ‘模块名称’ 创建帖子路由
nest g controller posts
创建后的posts模块,会自动关联起来。
posts.controller.ts文件
import { Controller, Get, Post } from '@nestjs/common';
@Controller('posts')
export class PostsController {
@Get()
index() {
return [
{id:1111},
{id:13},
{id:14},
{id:1},
{id:37},
{id:26},
];
}
//post请求
@Post()
create(){
return{
success:true
}
}
//get请求带动态id
@Get(':id')
detail(){
return{
id:1,
title:'aaaaa'
}
}
}
posts.module.ts文件
import { Module } from '@nestjs/common';
import { PostsController } from './posts.controller';
@Module({
controllers: [PostsController]
})
export class PostsModule {}