Git Product home page Git Product logo

express-router-decorator's Introduction

데코레이터를 활용한 Express 라우터 생성

개요

데코레이터가 달린 컨트롤러를 구현하면

@Controller('/cats')
export class CatController {
  private cats: Array<{ name: string }> = [{ name: 'Tom' }, { name: 'Kitty' }];

  @Get('')
  public index(req: Request, res: Response): void {
    res.json({ cats: this.cats });
  }

  @Post('')
  public add(req: Request, res: Response): void {
    this.cats.push(req.body);
    res.status(204).json();
  }

  @Get('/:name')
  public findByName(req: Request, res: Response): unknown {
    const { name } = req.params;
    const foundCat = this.cats.find((c) => c.name === name);
    if (foundCat) {
      return res.json({ cat: foundCat });
    }

    return res.status(404).json({ message: 'Cat not found!' });
  }
}

서버 시작 시 라우터로 등록됨

┌─────────┬───────────────────┬────────────────────────────┐
│ (index) │        api        │          handler           │
├─────────┼───────────────────┼────────────────────────────┤
│    0    │    'GET /cats'    │   'CatController.index'    │
│    1    │   'POST /cats'    │    'CatController.add'     │
│    2    │ 'GET /cats/:name' │ 'CatController.findByName' │
└─────────┴───────────────────┴────────────────────────────┘

파일 설명

데코레이터

  • 컨트롤러 데코레이터
  • 클래스 상단에 추가
  • 기본경로를 지정할 때 사용
  • 메서드 데코레이터
  • 메서드 상단에 추가
  • HTTP 메서드와 세부 경로를 지정할 때 사용

컨트롤러

  • 컨트롤러 구현체
  • 클래스와 메서드에 데코레이터가 부착되어있음

애플리케이션

  • Express 애플리케이션 싱글턴 객체를 반환
  • 컨트롤러에 달린 메타데이터 가져와서 라우터에 등록함

서버

  • Express 애플리케이션을 실행함

작동 원리

reflect-metadata를 사용한다.

Reflect.defineMetadata(키, 값, 타겟_객체)로 객체의 프로토타입에 원하는 정보를 저장할 수 있다.

Reflect.getMetadata(키, 타겟_객체)로 객체에 저장된 정보를 가져와서 원하는 처리(라우터 등록, 트랜잭션 등등)를 수행할 수 있다.

데코레이터는 객체 정의 시점에 실행되며

클래스 데코레이터는 생성자를 인자로

메서드 데코레이터는 클래스의 프로토타입과 메서드의 이름을 인자로 받는다.

받은 인자와 Reflect를 활용해서 메타데이터를 객체의 프로토타입에 저장하고, 나중에 꺼내서 각종 처리를 하면 된다.

express-router-decorator's People

Contributors

kakao-mark-down avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.