main.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { NestFactory } from '@nestjs/core';
  2. import { AppModule } from './app.module';
  3. import { serverConfig } from './config/config';
  4. import { join } from 'path';
  5. import * as fs from 'fs';
  6. import { NestExpressApplication } from '@nestjs/platform-express';
  7. import session from 'express-session';
  8. import { ValidationPipe } from '@nestjs/common';
  9. async function bootstrap() {
  10. const certsDir = join(__dirname, '..', 'certs');
  11. const httpsOptions = {
  12. key: fs.readFileSync(join(certsDir, 'local-key.pem')),
  13. cert: fs.readFileSync(join(certsDir, 'local-cert.pem')),
  14. };
  15. // const app = await NestFactory.create<NestExpressApplication>(AppModule);
  16. // const app = await NestFactory.create<NestExpressApplication>(AppModule, {
  17. // httpsOptions, // <-- Let Nest bind HTTPS
  18. // });
  19. const app = await NestFactory.create<NestExpressApplication>(AppModule);
  20. app.useGlobalPipes(
  21. new ValidationPipe({
  22. whitelist: true, // strips extra fields
  23. forbidNonWhitelisted: true, // rejects unknown fields
  24. transform: true, // converts types (e.g. string -> number/date)
  25. }),
  26. );
  27. app.enableCors({
  28. origin: 'http://localhost:4200', // your Angular app URL
  29. credentials: true,
  30. });
  31. app.setGlobalPrefix('api');
  32. const angularDistPath = join(__dirname, '..', '..', 'web-app', 'dist', 'mobile-auth-web-app', 'browser');
  33. const indexPath = join(angularDistPath, 'index.html');
  34. app.useStaticAssets(angularDistPath);
  35. app.setBaseViewsDir(angularDistPath);
  36. app.setViewEngine('html');
  37. app.use(
  38. session({
  39. secret: 'your-secret',
  40. resave: false,
  41. saveUninitialized: false,
  42. cookie: {
  43. httpOnly: true, // browser can’t access cookie via JS
  44. secure: false, // set true if using HTTPS
  45. sameSite: 'lax', // allow sending cookies cross-origin on localhost
  46. maxAge: 24 * 60 * 60 * 1000, // 1 day
  47. },
  48. }),
  49. );
  50. app.use((req, res, next) => {
  51. const isStaticAsset = req.url.includes('.');
  52. const isApiCall = req.url.startsWith('/api') || req.method !== 'GET';
  53. if (isStaticAsset || isApiCall) return next();
  54. res.sendFile(indexPath);
  55. });
  56. await app.listen(4000, '0.0.0.0');
  57. console.log(`🚀 HTTPS server running at ${serverConfig.exposedUrl}`);
  58. }
  59. bootstrap();