main.ts43 lines, 1.2 KB
| 1 | import { NestFactory } from '@nestjs/core'; |
| 2 | import { AppModule } from './app.module'; |
| 3 | import { ValidationPipe } from '@nestjs/common'; |
| 4 | import { |
| 5 | AllExceptionsFilter, |
| 6 | HttpExceptionFilter, |
| 7 | SERVICES_PORTS, |
| 8 | TransformInterceptor, |
| 9 | } from '@app/common'; |
| 10 | |
| 11 | async function bootstrap() { |
| 12 | const app = await NestFactory.create(AppModule); |
| 13 | |
| 14 | app.enableCors({ |
| 15 | origin: ['http://localhost:4000', 'https://mytrusteddomain.com'].filter( |
| 16 | Boolean, |
| 17 | ), |
| 18 | credentials: true, |
| 19 | methods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE', 'OPTIONS'], |
| 20 | allowedHeaders: ['Content-Type', 'Authorization'], |
| 21 | }); |
| 22 | |
| 23 | //Enable validation |
| 24 | app.useGlobalPipes( |
| 25 | new ValidationPipe({ |
| 26 | whitelist: true, |
| 27 | transform: true, |
| 28 | forbidNonWhitelisted: true, |
| 29 | }), |
| 30 | ); |
| 31 | |
| 32 | // Global interceptors, filters, and other middlewares can be set up here |
| 33 | app.useGlobalInterceptors(new TransformInterceptor()); |
| 34 | |
| 35 | //Note: Filters are applied in a reversed order |
| 36 | app.useGlobalFilters(new AllExceptionsFilter(), new HttpExceptionFilter()); |
| 37 | |
| 38 | await app.listen(process.env.PORT || SERVICES_PORTS.API_GATEWAY); |
| 39 | console.log( |
| 40 | `API Gateway is running on port ${process.env.PORT || SERVICES_PORTS.API_GATEWAY}`, |
| 41 | ); |
| 42 | } |
| 43 | bootstrap(); |