DEV Community

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

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

Neon image

Serverless Postgres in 300ms (❗️)

10 free databases with autoscaling, scale-to-zero, and read replicas. Start building without infrastructure headaches. No credit card needed.

Try for Free →

Top comments (0)

Neon image

Set up a Neon project in seconds and connect from a Node.js application

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay