File size: 1,527 Bytes
97dab2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestExpressApplication } from '@nestjs/platform-express';
import * as expressLayouts from 'express-ejs-layouts';
import { join } from 'path';
import * as session from 'express-session';
import * as cookieParser from 'cookie-parser';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  const isProduction = process.env.NODE_ENV === 'production';

  app.setBaseViewsDir(join(__dirname, '..', 'src', 'views'));
  app.useStaticAssets(join(__dirname, '..','src', 'public'));
  app.use(expressLayouts); // Enable express-ejs-layouts
  app.setViewEngine('ejs');
  app.set('layout', '_layouts/default');

  //app.setGlobalPrefix('api'); // 👈 This sets /api as the prefix

  app.enableCors({
    origin: /http:\/\/localhost:\d+$/, // Allow all requests from localhost on any port
    credentials: true,                  // Enable credentials if needed (cookies, auth headers, etc.)
  });

  app.use(
    session({
      secret: process.env.SESSION_SECRET || 'your-secret-key', // Replace with a secure secret
      resave: false,
      saveUninitialized: false,
      cookie: {
        httpOnly: true,
        secure: isProduction, // Use HTTPS in production
        maxAge: 24 * 60 * 60 * 1000, // 1 day
      },
    }),
  );
  app.use(cookieParser());

  await app.listen(process.env.PORT || 3004);
  console.log(`Application is running on: ${await app.getUrl()}`);
}
bootstrap();