From 1210ba2d15e3b03028414555737c2971f101bac0 Mon Sep 17 00:00:00 2001 From: Bastian Wagner Date: Tue, 18 Aug 2026 18:13:48 +0200 Subject: [PATCH] fix: prefix API routes --- apps/api/src/app.config.ts | 5 +++++ apps/api/src/main.ts | 2 ++ apps/api/test/app.e2e-spec.ts | 21 +++++++++++++++++---- 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 apps/api/src/app.config.ts diff --git a/apps/api/src/app.config.ts b/apps/api/src/app.config.ts new file mode 100644 index 0000000..1a4dc9e --- /dev/null +++ b/apps/api/src/app.config.ts @@ -0,0 +1,5 @@ +import { INestApplication } from '@nestjs/common'; + +export function configureApplication(app: INestApplication): void { + app.setGlobalPrefix('api'); +} diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index f76bc8d..7cce8dc 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -1,8 +1,10 @@ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; +import { configureApplication } from './app.config'; async function bootstrap() { const app = await NestFactory.create(AppModule); + configureApplication(app); await app.listen(process.env.PORT ?? 3000); } bootstrap(); diff --git a/apps/api/test/app.e2e-spec.ts b/apps/api/test/app.e2e-spec.ts index a767839..944f6f7 100644 --- a/apps/api/test/app.e2e-spec.ts +++ b/apps/api/test/app.e2e-spec.ts @@ -1,8 +1,13 @@ import { Test, TestingModule } from '@nestjs/testing'; -import { INestApplication } from '@nestjs/common'; +import { INestApplication, Module } from '@nestjs/common'; import request from 'supertest'; import { App } from 'supertest/types'; import { AppModule } from './../src/app.module'; +import { DatabaseModule } from './../src/database/database.module'; +import { configureApplication } from './../src/app.config'; + +@Module({}) +class TestDatabaseModule {} describe('AppController (e2e)', () => { let app: INestApplication; @@ -10,19 +15,27 @@ describe('AppController (e2e)', () => { beforeEach(async () => { const moduleFixture: TestingModule = await Test.createTestingModule({ imports: [AppModule], - }).compile(); + }) + .overrideModule(DatabaseModule) + .useModule(TestDatabaseModule) + .compile(); app = moduleFixture.createNestApplication(); + configureApplication(app); await app.init(); }); - it('/ (GET)', () => { + it('/api (GET)', () => { return request(app.getHttpServer()) - .get('/') + .get('/api') .expect(200) .expect('Hello World!'); }); + it('/ (GET) is not an API route', () => { + return request(app.getHttpServer()).get('/').expect(404); + }); + afterEach(async () => { await app.close(); });