Real World Example

A Startup API Bootstrap

A typical production main.ts at an Indian D2C startup: global prefix, versioning, validation, CORS for the storefront domain and graceful shutdown for deployments.

src/main.ts

import { Logger, ValidationPipe, VersioningType } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.setGlobalPrefix('api');
  app.enableVersioning({ type: VersioningType.URI, defaultVersion: '1' });   // /api/v1/...
  app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));
  app.enableCors({ origin: ['https://shopease.in', 'http://localhost:3001'] });
  app.enableShutdownHooks();                       // close DB connections on SIGTERM

  const port = Number(process.env.PORT ?? 3000);
  await app.listen(port);
  Logger.log(`ShopEase API running on port ${port}`, 'Bootstrap');
}
bootstrap();

Example

curl -i http://localhost:3000/api/v1/health

Example output

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8

{"status":"ok"}