main.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { NestFactory } from '@nestjs/core'
  2. import { AppModule } from './app.module'
  3. import { Logger, ValidationPipe } from '@nestjs/common'
  4. import { ConfigService } from '@nestjs/config'
  5. import { configureSwaggerDocs } from './helpers/configure-swagger-docs.helper'
  6. import { NestExpressApplication } from '@nestjs/platform-express'
  7. import { join } from 'path'
  8. async function bootstrap() {
  9. const app = await NestFactory.create<NestExpressApplication>(AppModule, {
  10. snapshot: true,
  11. abortOnError: true
  12. })
  13. const configService = app.get<ConfigService>(ConfigService)
  14. app.setGlobalPrefix('api')
  15. app.useGlobalPipes(
  16. new ValidationPipe({
  17. whitelist: false,
  18. transform: true,
  19. forbidNonWhitelisted: false,
  20. transformOptions: {
  21. enableImplicitConversion: true
  22. }
  23. })
  24. )
  25. configureSwaggerDocs(app, configService)
  26. app.enableCors({
  27. origin: true,
  28. methods: 'GET,POST,PUT,PATCH,DELETE',
  29. credentials: true
  30. })
  31. app.useStaticAssets(join(__dirname, '..', 'public'))
  32. app.setBaseViewsDir(join(__dirname, '..', 'views'))
  33. app.setViewEngine('hbs')
  34. const port = configService.get<number>('NODE_API_PORT') || 3000
  35. await app.listen(port)
  36. Logger.log(`Url for OpenApi: ${await app.getUrl()}/docs`, 'Swagger')
  37. }
  38. bootstrap().catch((err) => {
  39. Logger.error(err, 'Bootstrap')
  40. process.exit(1)
  41. })