如何使用tsoa与express



在express.js中我有这样定义的控制器:

@Route('notes')
export default class NoteController extends Controller {
@Get('/')
public async (req: Request, res: Response, _next: NextFunction): Promise<any> => {
try {
const Note: CreateNote = req.body;
await notesQueries.createNote(Note);
return res.status(200).send({
data: {},
code: 200,
message: 'Note created!'
});
} catch (e) {
console.log(e);
return res.status(400).send({
data: {},
code: 400,
message: 'Something happened on creating Note!'
});
}
};

当我运行tsoa spec生成swagger时。我得到以下错误信息:

There was a problem resolving type of 'Request'.
Generate swagger error.
GenerateMetadataError: Cannot read properties of undefined (reading 'text')

试着找出我做错了什么?有人知道吗?

我遇到了同样的问题,使用tsoa文档修复,他们建议在dist文件夹中生成路由,并在app上使用RegisterRoutes, tsoa会用Route()装饰符注册所有控制器。

// tsoa.json
{
"entryFile": "src/app.ts",
"noImplicitAdditionalProperties": "throw-on-extras",
"controllerPathGlobs": ["src/**/*Controller.ts"],
"spec": {
"outputDirectory": "build",
"specVersion": 3
},
"routes": {
"routesDir": "build"
}
}

和在应用程序:

// src/app.ts
import express, {json, urlencoded} from "express";
import { RegisterRoutes } from "../build/routes";
export const app = express();
// Use body parser to read sent json payloads
app.use(
urlencoded({
extended: true,
})
);
app.use(json());
RegisterRoutes(app);

相关内容

  • 没有找到相关文章

最新更新