swagger集成_将swagger与nestjs集成

swagger集成In this article, we will integrate Swagger with a RESTful API built using NestJS. As a quick refresher, NestJS is a server-side web framework that supports both TypeScript and JavaScript. It ...
摘要由CSDN通过智能技术生成

swagger集成

In this article, we will integrate Swagger with a RESTful API built using NestJS. As a quick refresher, NestJS is a server-side web framework that supports both TypeScript and JavaScript. It brings an opinionated architecture to building a Node.js backend that is inspired by the Angular ecosystem. For more information, check out this introduction to NestJS.

在本文中,我们将Swagger与使用NestJS构建的RESTful API集成在一起。 作为快速更新,NestJS是一个服务器端Web框架,同时支持TypeScript和JavaScript。 它为基于Angular生态系统的Node.js后端构建了一个自以为是的体系结构。 有关更多信息, 请查看NestJS简介

On the other hand, Swagger is a tool for implementing the OpenAPI specification. The goal of the OpenAPI specification is to provide a standardized way to understand the capabilities of an API service without actual access to the source code. On the whole, Swagger is a tremendously helpful documentation tool for any modern web backend that exposes a RESTful API. More than just documentation, the OpenAPI specification served up by Swagger can be used to autogenerate servers/clients, facilitate automated testing, and much more.

另一方面, Swagger是用于实现OpenAPI规范的工具。 OpenAPI规范的目标是提供一种无需实际访问源代码即可了解API服务功能的标准化方法。 总体而言,Swagger对于公开了RESTful API的任何现代Web后端都是非常有用的文档工具。 Swagger提供的OpenAPI规范不只是文档,还可以用于自动生成服务器/客户端,促进自动化测试等等。

In the following sections, we’ll add Swagger to a basic RESTful API built using NestJS.

在以下各节中,我们将Swagger添加到使用NestJS构建的基本RESTful API中。

最初设定 (Initial Setup)

The first dependency that we’ll need is the NestJS CLI. Go ahead and install it globally by running npm install -g @nestjs/cli. Once this is in place, bootstrap a new NestJS project using the CLI command nest new nestjs-with-swagger. At this point, we should have a boilerplate NestJS project setup with a module, controller, and service.

我们需要的第一个依赖项是NestJS CLI 。 继续并通过运行npm install -g @nestjs/cli全局npm install -g @nestjs/cli 。 安装到位后,使用CLI命令nest new nestjs-with-swagger引导新的NestJS项目。 在这一点上,我们应该有一个带有模块,控制器和服务的样板NestJS项目设置。

We’ll now create a simple task tracking application by adding a new module for handling tasks and a few other files, as shown below:

现在,我们将通过添加一个用于处理任务的新模块和一些其他文件来创建一个简单的任务跟踪应用程序,如下所示:

@Controller('task')
export class TaskController {
  constructor(private readonly taskService: TaskService) { }


  @Get()
  getTasks(): Task[] {
    return this.taskService.getTasks();
  }


  @Get(':id')
  getTask(@Param('id') taskId: string): Task {
    return this.taskService.getTask(taskId);
  }




  @Post()
  createTask(@Body() createTask: CreateTask): Task {
    return this.taskService.createTask(createTask)
  }
}
@Injectable()
export class TaskService {
  private tasks: Task[] = [];


  createTask(createTask: CreateTask): Task {
    const { title, description } = createTask;


    const newTask: Task = {
      id: uuidv4(),
      title,
      description,
      dateTimeCreated: moment().format(),
    }


    this.tasks.push(newTask)


    return newTask;
  }


  getTasks(): Task[] {
    return this.tasks;
  }


  getTask(taskId: string): Task {
    const task = this.tasks.find(task => tas
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值