import { NestFactory } from '@nestjs/core' ; import { Logger , InternalServerErrorException } from '@nestjs/common' ; import chalk from 'chalk' ; import { SwaggerModule , DocumentBuilder } from '@nestjs/swagger' ; import * as bodyParser from 'body-parser' ; import * as helmet from 'helmet' ; import * as rateLimit from 'express-rate-limit' ; import * as fs from 'fs' ; import { AppModule } from './app.module' ; import { ValidationPipe , LoggerMiddleware , TimeoutInterceptor , LoggingInterceptor , HttpExceptionFilter } from './common' ; import { MyLogger } from './config' ; import { NODE_ENV , DOMAIN , PORT } from './environments' ; declare const module ; async function bootstrap ( ) { try { const app = await NestFactory . create ( AppModule , { logger : new MyLogger ( ) , cors : true } ) ; app . use ( helmet ( ) ) ; app . use ( bodyParser . json ( ) ) ; app . use ( bodyParser . urlencoded ( ) ) ; app . use ( rateLimit ( { windowMs : 15 * 60 * 1000 , max : 10000 } ) ) ; const httpAdapter = app . getHttpAdapter ( ) ; NODE_ENV !== 'testing' && app . use ( LoggerMiddleware ) ; app . useGlobalPipes ( new ValidationPipe ( ) ) ; app . enableShutdownHooks ( ) ; const options = new DocumentBuilder ( ) . setTitle ( 'Nestjs Restful Best Practice' ) . setVersion ( '3.0.0' ) . setBasePath ( '/v1' ) . setDescription ( 'built NestJS, TypeORM, MongoDB' ) . setTermsOfService ( 'https://github.com/chnirt/nestjs-restful-best-practice/blob/master/LICENSE' ) . setContactEmail ( 'trinhchinchin@gmail.com' ) . setLicense ( 'MIT License' , 'https://github.com/chnirt/nestjs-restful-best-practice/blob/master/LICENSE' ) . setExternalDoc ( 'For more information' , 'http://swagger.io' ) . setSchemes ( NODE_ENV !== 'production' ? 'http' : 'https' ) . addBearerAuth ( 'Authorization' , 'header' ) . addTag ( 'chnirt' , 'developer' ) . build ( ) ; const document = SwaggerModule . createDocument ( app , options ) ; SwaggerModule . setup ( 'api' , app , document ) ; app . setGlobalPrefix ( '/v1' ) ; const server = await app . listen ( PORT ! ) ; if ( module . hot ) { module . hot . accept ( ) ; module . hot . dispose ( ( ) => app . close ( ) ) ; } NODE_ENV !== 'production' ? Logger . log ( `🚀  Server ready at https://${ DOMAIN ! }:${ chalk . hex ( '#87e8de' ) . bold ( `${ PORT ! }` ) }` , 'Bootstrap' ) : Logger . log ( `🚀  Server is listening on port ${ chalk . hex ( '#87e8de' ) . bold ( `${ PORT ! }` ) }` , 'Bootstrap' ) ; } catch ( error ) { Logger . error ( `❌  Error starting server, ${ error }` , '' , 'Bootstrap' , false ) ; process . exit ( ) ; throw new InternalServerErrorException ( error ) ; } } bootstrap ( ) . catch ( e => { throw e ; } ) ;