diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index ee84ced..b9c79ba 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -1,20 +1,34 @@ -/** - * This is not a production server yet! - * This is only a minimal backend to get started. - */ - import { Logger } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; import { AppModule } from './app/app.module'; +import { join } from 'path'; +import * as express from 'express'; +import { Request, Response, NextFunction } from 'express'; async function bootstrap() { const app = await NestFactory.create(AppModule); + const globalPrefix = 'api'; app.setGlobalPrefix(globalPrefix); + + const server = app.getHttpAdapter().getInstance(); + + const distPath = join(__dirname, '..', 'web'); + server.use(express.static(distPath)); + + server.use((req: Request, res: Response, next: NextFunction) => { + if (!req.path.startsWith(`/${globalPrefix}`)) { + res.sendFile(join(distPath, 'index.html')); + } else { + next(); + } + }); + const port = process.env.PORT || 3000; await app.listen(port); + Logger.log( - `🚀 Application is running on: http://localhost:${port}/${globalPrefix}` + `🚀 Application is running on: http://localhost:${port}/ (API prefix: /${globalPrefix})`, ); }