Express
方案一
// 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();
方案二:
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
const server = app.getHttpServer();
const router = server._events.request._router;
const availableRoutes: [] = router.stack
.map(layer => {
if (layer.route) {
return {
route: {
path: layer.route?.path,
method: layer.route?.stack[0].method,
},
};
}
})
.filter(item => item !== undefined);
console.log(availableRoutes);
}
bootstrap();
注意以上的两种方案都是在
app.listen
之后添加代码。
Fastify
在main.ts
文件的app.listen
之前插入如下代码:
app
.getHttpAdapter()
.getInstance()
.addHook('onRoute', opts => {
console.log(opts.url)
})
正文完