diff --git a/apps/backend/src/main.ts b/apps/backend/src/main.ts index 7c7551b..75b80bc 100644 --- a/apps/backend/src/main.ts +++ b/apps/backend/src/main.ts @@ -1,4 +1,3 @@ -import { join } from 'node:path'; import cookieParser from 'cookie-parser'; import type { NextFunction, Request, Response } from 'express'; import helmet from 'helmet'; @@ -11,6 +10,7 @@ import { AppModule } from './app.module'; import { createValidationPipe } from './common/security/validation.pipe'; import { AppConfigService } from './config/config.service'; import { MigrationHealthService } from './database/migration-health.service'; +import { resolvePublicAssetPath } from './static-assets'; async function bootstrap() { const app = await NestFactory.create(AppModule, { @@ -87,7 +87,7 @@ async function bootstrap() { }); } - app.useStaticAssets(join(__dirname, '..', 'public'), { + app.useStaticAssets(resolvePublicAssetPath(__dirname), { index: false, fallthrough: true, }); @@ -96,7 +96,7 @@ async function bootstrap() { next(); return; } - res.sendFile(join(__dirname, '..', 'public', 'index.html')); + res.sendFile(resolvePublicAssetPath(__dirname, 'index.html')); }); await app.get(MigrationHealthService).assertNoPendingMigrations(); diff --git a/apps/backend/src/static-assets.spec.ts b/apps/backend/src/static-assets.spec.ts new file mode 100644 index 0000000..4c9a979 --- /dev/null +++ b/apps/backend/src/static-assets.spec.ts @@ -0,0 +1,16 @@ +import { join } from 'node:path'; +import { describe, expect, it } from 'vitest'; +import { resolvePublicAssetPath } from './static-assets'; + +describe('resolvePublicAssetPath', () => { + it('resolves frontend files next to the compiled backend entrypoint', () => { + const runtimeDirectory = join('app', 'apps', 'backend', 'dist'); + + expect(resolvePublicAssetPath(runtimeDirectory)).toBe( + join(runtimeDirectory, 'public'), + ); + expect(resolvePublicAssetPath(runtimeDirectory, 'index.html')).toBe( + join(runtimeDirectory, 'public', 'index.html'), + ); + }); +}); diff --git a/apps/backend/src/static-assets.ts b/apps/backend/src/static-assets.ts new file mode 100644 index 0000000..3fe0616 --- /dev/null +++ b/apps/backend/src/static-assets.ts @@ -0,0 +1,8 @@ +import { join } from 'node:path'; + +export function resolvePublicAssetPath( + runtimeDirectory: string, + ...segments: string[] +): string { + return join(runtimeDirectory, 'public', ...segments); +}