NESTJS][CheatSheet]

·2 min read
  1. main.ts

const server = express();

const app = await NestFactory.create<NestExpressApplication>(###Module, new ExpressAdapter(server));

  • create의 type : NestExpressApplication이다.

  • ###Module : Entry (root) application module class

  • new ExpressAdapter(server) : Adapter to proxy the request/response cycle to the underlying HTTP server

 

app.getHttpAdapter().getInstance().set('maxResponseLength', '50mb');

  • getHttpAdapter() : Returns the underlying HTTP adapter. / To get a reference to the HttpAdapter from outside of the

 

application context, call the "getHttpAdapter()" method. | application에 외부의 HttpAdapter의 reference를 가져다 준다, 여기서는 express()인 server.

  • getInstance() : The adapter object exposes several useful methods to interact with the HTTP server. However, if you want to access the library instance (e.g., the Express instance) directly, call the getInstance() method. | express()인 server의 instance에 직접 접근할 수 있다.

set() : A wrapper function around native 'express.set()' method.

 

app.setGlobalPrefix('api'); : HTTP 응용 프로그램에 등록된 각 라우터에 대한 접두사를 한번에 설명하는 method이다.

 

app.useLogger(app.get(LOGGER_INSTANCE_TOKEN); : Sets custom logger service.

 

const swaggerConfig = new DocumentBuilder()...build();

const document = SwaggerModule.createDocument(app, swaggerConfig);

SwaggerModule.setup('api', app, document);

 

app.use(helmet());

 

await app.init();

  • init() : Initalizes the Nest application. Calls the Nest lifecycle events. It isn't mandatory to call this method directly.

 

const post = app.get(ConfigService).get<number>('port');

const post = app.get(ConfigService).get<boolean>('ssl.enabled');

  • apps > chain > re