Issue
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!
Solution
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();
Answered By - Mr. DMX
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.