Node.js 指定控制器的返回内容类型
我将以下内容作为Node.js 指定控制器的返回内容类型,node.js,typescript,swagger,Node.js,Typescript,Swagger,我将以下内容作为metrics.controller.ts文件: import { Controller, Get } from '@nestjs/common'; import { ApiOperation, ApiResponse, ApiUseTags, ApiModelProperty } from '@nestjs/swagger'; import { PrometheusService } from './prometheus.service'; @Controller('metr
metrics.controller.ts
文件:
import { Controller, Get } from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiUseTags, ApiModelProperty } from '@nestjs/swagger';
import { PrometheusService } from './prometheus.service';
@Controller('metrics')
@ApiUseTags('Misc')
export class MetricsController {
constructor(private readonly prometheusService: PrometheusService) {}
@Get('/')
@ApiOperation({
title: 'Prometheus metrics',
description: 'Exposes default prometheus node metrics'
})
@ApiResponse({ status: 200, description: 'Prometheus metrics' })
public getMetrics(): string {
return this.prometheusService.getMetrics();
}
}
但是,当实际输出为文本/纯文本时,Swagger错误地表示响应内容类型为应用程序/json:
我试着看了一下,我要找的是“内容”部分。但是,该文档不包括TypeScript注释。上面提到了一个@操作
注释,但我没有这方面的参考资料,也不知道在哪里可以找到它——只有@ApiOperation
起作用
项目目录中npm ls的相关版本信息如下:
+-- @nestjs/swagger@2.5.1
| +-- @nestjs/common@5.3.6 deduped
| +-- @nestjs/core@5.3.6 deduped
| +-- lodash@4.17.10
| +-- path-to-regexp@2.2.1 deduped
| `-- swagger-ui-express@3.0.10
什么是表示文本/普通的响应内容类型的正确注释?我发现解决方案是
从'@nestjs/swagger'导入APIproducts
,如下所示:
import { ApiOperation, ApiResponse, ApiUseTags, ApiProduces } from '@nestjs/swagger'
//...
@ApiProduces('text/plain')
public getMetrics(): string {
//...
}
我发现解决方案是
从'@nestjs/swagger'导入APIproducts
,如下所示:
import { ApiOperation, ApiResponse, ApiUseTags, ApiProduces } from '@nestjs/swagger'
//...
@ApiProduces('text/plain')
public getMetrics(): string {
//...
}