main.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { NestFactory } from '@nestjs/core';
  2. import { AppModule } from './app.module';
  3. import { serverConfig } from './config';
  4. import * as session from 'express-session';
  5. import { join } from 'path';
  6. import { NestExpressApplication } from '@nestjs/platform-express';
  7. import * as fs from 'fs';
  8. async function bootstrap() {
  9. const app = await NestFactory.create<NestExpressApplication>(AppModule);
  10. app.enableCors();
  11. // ✅ API prefix
  12. app.setGlobalPrefix('api'); // <<==== All controllers now under /api/*
  13. const angularDistPath = join(__dirname, '..', '..', 'web-app', 'dist', 'mobile-auth-web-app', 'browser');
  14. const indexPath = join(angularDistPath, 'index.html');
  15. console.log('✅ Angular path:', angularDistPath);
  16. console.log('✅ index.html exists:', fs.existsSync(indexPath));
  17. // ✅ Now serve Angular static files
  18. app.useStaticAssets(angularDistPath);
  19. app.setBaseViewsDir(angularDistPath);
  20. app.setViewEngine('html');
  21. app.use(
  22. session({
  23. secret: 'your-secret',
  24. resave: false,
  25. saveUninitialized: false,
  26. }),
  27. );
  28. // ✅ Angular fallback: only for non-API, non-static requests
  29. app.use((req, res, next) => {
  30. const isStaticAsset = req.url.includes('.');
  31. const isApiCall = req.url.startsWith('/api') || req.method !== 'GET';
  32. if (isStaticAsset || isApiCall) {
  33. return next();
  34. }
  35. console.log('📦 Angular fallback hit for:', req.url);
  36. res.sendFile(indexPath);
  37. });
  38. await app.listen(process.env.PORT ?? 3000);
  39. console.log(`🚀 Server running at http://localhost:3000`);
  40. console.log(`🌐 Exposed at: ${serverConfig.exposedUrl}`);
  41. }
  42. bootstrap();