我在 main.ts 中添加了以下代码
import { NestFactory } from '@nestjs/core';
import{SwaggerModule,DocumentBuilder} from '@nestjs/swagger'
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const options = new DocumentBuilder()
.setTitle('My API')
.setDescription('API description')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('api', app, document);
await app.listen(3000);
}
bootstrap();
//controller
@Post()
addProduct(
@Body('title') title:string,
@Body('price') price:number,
):any{
const idFromService=this.productserive.addNewProduct(title,price);
return idFromService;
}
//productservice
export class ProductService {
products:Product[]=[];
addNewProduct(title:string,price:number){
const id=uId();
const newproduct=new Product(id,title,price);
this.products.push(newproduct);
return {title,price};
}
}