如何在Nestjs中获取所有路由(从每个模块上的所有模块和控制器)? [英] How can I get all the routes (from all the modules and controllers available on each module) in Nestjs?
本文介绍了如何在Nestjs中获取所有路由(从每个模块上的所有模块和控制器)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Nestjs,我想获取带有HTTP动词的所有可用路由(控制器方法)的列表,如下所示:
API:POST/api/v1/userGET/api/v1/userPUT/api/v1/用户
似乎需要访问Express路由器,但是我还没有在Nestjs中找到一种方法来做到这一点.为了表达意见,有一些库,例如"
Using Nestjs I'd like to get a list of all the available routes (controller methods) with http verbs, like this:
API:
POST /api/v1/user
GET /api/v1/user
PUT /api/v1/user
It seems that access to express router is required, but I haven found a way to do this in Nestjs. For express there are some libraries like "express-list-routes" or "express-list-endpoints".
Thanks in advance!
解决方案
I just found that Nestjs app has a "getHttpServer()" method, with this I was able to access the "router stack", here's the solution:
// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as expressListRoutes from 'express-list-routes';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors();
await app.listen(3000);
const server = app.getHttpServer();
const router = server._events.request._router;
console.log(expressListRoutes({}, 'API:', router));
}
bootstrap();
这篇关于如何在Nestjs中获取所有路由(从每个模块上的所有模块和控制器)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文