我为毕业项目创建了一个简单的金融 API,我正在尝试使用 Swagger 来记录该 API。但在 Swagger 上却出现了这个错误。有人可以帮助我吗?
有问题的错误:
{
"statusCode": 500,
"error": "Internal Server Error",
"message": "Cannot assign to read only property 'name' of function 'function () { [native code] }'"
}
我的代码:
import fastify from 'fastify';
import {
serializerCompiler,
validatorCompiler,
type ZodTypeProvider,
} from 'fastify-type-provider-zod'
import fastifyCors from '@fastify/cors'
import fastifySwagger from '@fastify/swagger'
import fastifySwaggerUi from '@fastify/swagger-ui'
const app = fastify().withTypeProvider<ZodTypeProvider>()
app.register(fastifyCors, {
origin: '*',
})
app.setValidatorCompiler(validatorCompiler)
app.setSerializerCompiler(serializerCompiler)
app.register(fastifySwagger, {
swagger: {
info: {
title: 'API de Transações e Projeções',
description: 'Documentação da API',
version: '1.0.0',
},
schemes: ['http'],
consumes: ['application/json'],
produces: ['application/json'],
}
})
app.register(fastifySwaggerUi, {
routePrefix: '/docs',
uiConfig: {
docExpansion: 'full',
deepLinking: false,
}
})
app.listen({
port: process.env.PORT ? Number(process.env.PORT) : 3333
}).then(() => {
console.log('HTTP server running')
console.log(`Server is running on port ${process.env.PORT ? Number(process.env.PORT) : 3333}`)
})
我尝试使用 Swagger 来记录 fastify API,但收到一个我无法理解的错误。
const express = require('express'); const swaggerUi = require('swagger-ui-express');
const app = express();