From 20241b6ce1bc5932fa1ba4e05fd5b462eaec987c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D0=BE=D0=B2=20=D0=90=D1=80=D1=82?= =?UTF-8?q?=D0=B5=D0=BC?= Date: Mon, 20 Apr 2026 23:08:21 +0300 Subject: [PATCH] Add global API prefix and route remapping for health checks in main.ts --- server/src/main.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/server/src/main.ts b/server/src/main.ts index b57742c..5bf9ebf 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -1,5 +1,6 @@ -import { ValidationPipe } from '@nestjs/common'; +import { RequestMethod, ValidationPipe } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; +import type { NextFunction, Request, Response } from 'express'; import { AppModule } from './app.module'; async function bootstrap() { @@ -21,6 +22,25 @@ async function bootstrap() { }), ); + // Prefer a stable `/api/*` surface in all environments. + // Some deployments/proxies may strip `/api` (e.g. nginx rewrite), so we also + // accept legacy non-prefixed routes by internally re-mapping them. + app.setGlobalPrefix('api', { + exclude: [{ path: 'health', method: RequestMethod.ALL }], + }); + app.use((req: Request, _res: Response, next: NextFunction) => { + if (req.url === '/health' || req.url.startsWith('/health?') || req.url.startsWith('/api/')) { + return next(); + } + if (req.url === '/api' || req.url.startsWith('/api?')) { + req.url = req.url.replace(/^\/api/, '/api/'); + return next(); + } + + req.url = `/api${req.url.startsWith('/') ? '' : '/'}${req.url}`; + return next(); + }); + await app.listen(process.env.PORT ?? 3000); }