DEV Community

Cover image for Get a list of endpoints in your NestJS app

Get a list of endpoints in your NestJS app

So here is how I get a list of all defined HTTP endpoints in my E2E test in a NestJS app with their default configuration and setup:

import { Test, TestingModule } from '@nestjs/testing';
import { SomeModule } from './some/where/some.module';

const moduleFixture: TestingModule = await Test.createTestingModule({
 imports: [SomeModule],
}).compile();
const app = moduleFixture.createNestApplication();
await app.init();

console.log(
  app
    .getHttpServer()
    ._events.request.router.stack.filter((layer: any) => layer.route)
    .map((layer: any) => ({
      method: Object.keys(layer.route.methods)[0].toUpperCase(),
      path: layer.route.path,
    }))
)
Enter fullscreen mode Exit fullscreen mode

This is mostly useful when I am banging my head against a brick wall as to why supertest says an endpoint does not exists (the infamous 404 error). So feel free to use this hack whenever you stock in the same spot :).

BTW feel free to like this post, subscribe and comment.


Instagram: https://www.instagram.com/node.js.developers.kh/
Facebook: https://www.facebook.com/kasirbarati
X: https://x.com/kasir_barati
YouTube: https://www.youtube.com/@kasir-barati
GitHub: https://github.com/kasir-barati/
Dev.to: https://dev.to/kasir-barati
LinkedIn: https://linkedin.com/in/kasir-barati

Top comments (0)