웹 개발/🟩 Node.js

웹 개발/🟩 Node.js

NestJS | Interceptors & AOP 패턴

인터셉터 인터셉터는 @Injectable() 데코레이터로 주석이 달린 클래스이다 인터셉터는 NestInterceptor 인터페이스를 구현해야한다 인터셉터에는 AOP기술에서 영감을 받은 유용한 기능 세트가있는데 여기서 AOP는 aspect-oriented programming 관점지향 프로그래밍이다 모듈성을 증가 시키는 것이 목적인 프로그래밍 패러다임이다 그림으로 설명을하자면 핵심기능이 4개있다고 치자 기둥 하나하나가 모듈안에 있는 컨트롤러라고 생각해보자 예를들어 users controller,catch controller ,blog controller ,money controller 이렇게 쭉 실행된다고 하면 그 코드들에서 재사용이되는 공통적인 기능들이 있을텐데 가장 적합한 예가 바로 '로깅'이라는 기능..

웹 개발/🟩 Node.js

NestJS | Exception filter & Pipes

1. Exception filter Postman에서 존재하지않는 주소로 접속해보면 위와같이 message,error,statuscode 형식으로 nestJS에서 제공하는 형식으로 에러를 반환하는것을 확인할 수 있다 나중에 내 서비스를 제작할때 에러시 추가적으로 항목을 보여주고싶을수 있다 CatsController를 예시로 보자 import { CatsService } from './cats.service'; import { Controller, Get } from '@nestjs/common'; @Controller('cats') export class CatsController { constructor(private readonly catsService: CatsService) {} @Get() ge..

웹 개발/🟩 Node.js

NestJS | 미들웨어

1. 미들웨어 nest g middleware 미들웨어명 logger라는 이름으로 middleware를 생성해보자 nest g middleware logger 그러면 import { Injectable, NestMiddleware } from '@nestjs/common'; @Injectable() export class LoggerMiddleware implements NestMiddleware { use(req: any, res: any, next: () => void) { next(); } } 위와같이 미들웨어가 생성되고 express 에서 봤던 use가 보인다 req,res,next 타입을 express 의 request,response, nextFunction으로 변경해보자 import { In..

웹 개발/🟩 Node.js

NestJS | Modules & 캡슐화

1. 모듈 생성 VSCode 터미널에서 nest g mo 모듈명 모듈명은 일반적으로 복수형으로 짓는다 nest g mo cats 자동으로 cats 폴더가 생성되고 그안에 모듈이 생성된다 그리고 자동으로 import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { CatsModule } from './cats/cats.module'; @Module({ imports: [CatsModule], controllers: [AppController], providers: [AppService], }) export cla..

웹 개발/🟩 Node.js

NestJS | Providers & 의존성 주입(DI)

import { Body, Controller, Get, Param, Req } from '@nestjs/common'; import { AppService } from './app.service'; @Controller('cats') export class AppController { constructor(private readonly appService: AppService) {} @Get('hello/:id') getHello(@Req() req: Request, @Body() Body, @Param() param): string { console.log(req); console.log(param); return this.appService.getHello(); } } 컨트롤러 단에서 appServ..

웹 개발/🟩 Node.js

NestJS | 구조 & Controller 패턴

1. package.json @nestjs로 시작하는 애들은 nestjs 안에서 자체적으로 실행되는 라이브러리들 reflect-metadata 는 데코레이터에 사용되는 라이브러리 rxjs 는 비동기 및 이벤트기반 프로그래밍을 작성하기위한 라이브러리 2.controller app.controller import { Controller, Get } from '@nestjs/common'; import { AppService } from './app.service'; @Controller('cats') export class AppController { constructor(private readonly appService: AppService) {} @Get('hello') getHello(): string..

이재원
'웹 개발/🟩 Node.js' 카테고리의 글 목록