S SmartDocs
シリーズ: nest.js typescript 30 行 · 更新日 2026-02-03

main.ts

nest.js/01-beginner/demo-beginner/src/main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  
  // Enable CORS for frontend communication
  app.enableCors({
    origin: 'http://localhost:3000',
    credentials: true,
  });

  // Serve static files from public directory
  app.useStaticAssets(join(__dirname, '..', 'public'), {
    prefix: '/',
  });

  // Serve index.html at root
  app.setBaseViewsDir(join(__dirname, '..', 'public'));
  
  const port = process.env.PORT || 3000;
  await app.listen(port);
  console.log(`🚀 Application is running on: http://localhost:${port}`);
  console.log(`📱 Frontend available at: http://localhost:${port}/`);
}

bootstrap();

関連記事