조회 수 438 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
Extra Form
라이선스 MIT

이번에 새로 작업하고 있는 Authentication 서버를 만들기 위해서 Koa를 한 번 써보려고 하는데 Restify에서는 이미 Object에서 모든 Route를 추출하는 소스를 짜 두었지만 Koa의 경우에는 스펙이 약간 다르더군요. 사실은 거의 똑같습니다만 조금 더 좋게 만들어둔 소스코드입니다. 왜냐하면 이전에는 POST로 API 콜을 받을 일이 거의 없었기 때문...이긴 한데.


사실 이렇게 해서 나쁠 것은 없지만 가끔가다가 이상한 모듈이 섞여 있는 것을 조심해야 한다고 생각합니다. (한 번 당한 적이 있어서...)


그래도 직접 지정해주는 것보다는 몇 배 더 편하다고 생각하지만요.


const log = require('./log')
 
const autofill = (router, routes, prefix) => {
  prefix = prefix || ''
 
  if (typeof routes === 'object') {
    if (typeof routes.fn === 'function') {
      log(`Autofilled route for ${routes.method.toUpperCase()}:${prefix}.`)
 
      router[routes.method](prefix, routes.fn)
    } else {
      const sub = Object.keys(routes)
 
      for (let k = 0; k < sub.length; k++) {
        autofill(router, routes[sub[k]], prefix + '/' + sub[k])
      }
    }
  }
}
 
module.exports = autofill


> https://github.com/Seia-Soto/authenPoint/blob/master/structures/routing/autofill.js


에서 추출했습니당.


Koa-router와 핸들러 전체를 Object (아래와 같은 형식으로)로 제공하면 됩니다.


{

  test: { // /test

    a: { // /test/a 로 라우팅

      method: 'get',

      fn: async ctx => ctx.body = 'hello world'

    }

  }

}


  1. [PHP] 기상청 중기예보를 캐러셀로 보여주는 위젯 (매우 허접합니다 ㅠㅠ)

    Date2018.09.28 Category코드 By이니스프리 Views647
    Read More
  2. Cmd 에서 서비스 시작 / 종료하기

    Date2018.02.18 Category코드 ByProjectSE Views644
    Read More
  3. 브라우저 언어에 따라 다른 폴더를 사용하는 PHP 코드

    Date2017.10.10 Category코드 By네모 Views639
    Read More
  4. [아미나] 게시글을 작성하면 ID와 IP로 필터링하여 자동으로 랜덤 댓글을 남기기 (+랜덤 포인트)

    Date2018.11.18 Category코드 By이니스프리 Views634
    Read More
  5. [JS]클라이언트에서 Ip를 얻어보자

    Date2019.01.21 Category코드 ByHanam09 Views625
    Read More
  6. [PHP] 간단한 캐싱 클래스

    Date2018.12.06 Category코드 Bytitle: 황금 서버 (30일)humit Views605
    Read More
  7. 매우 특이한 버그

    Date2018.06.05 Category코드 Bytitle: 대한민국 국기gimmepoint Views569
    Read More
  8. Hello, World!를 출력해보자

    Date2018.04.21 Category코드 By네모 Views568
    Read More
  9. [Python] 모 정부기관 사이트 파싱 후 PC 통신처럼 열람하고 싶은 게시글 번호를 입력하면 내용을 보여주는 소스 (허접)

    Date2018.09.14 Category코드 By이니스프리 Views559
    Read More
  10. 내가 만든 사칙연산 계산기

    Date2018.05.11 Category코드 Bytitle: 대한민국 국기gimmepoint Views515
    Read More
  11. HEX를 RGB로, RGB를 HEX로 바꾸는 PHP 코드

    Date2018.05.05 Category코드 By네모 Views507
    Read More
  12. 파이선 셸에서 실행하면...?

    Date2018.07.22 Category코드 By제르엘 Views499
    Read More
  13. c 이진트리 전,중,후위 알고리즘

    Date2018.04.24 Category코드 Bytitle: 대한민국 국기gimmepoint Views495
    Read More
  14. 아주 간단한 기초 C++

    Date2018.04.21 Category코드 By제르엘 Views477
    Read More
  15. [JS] 클라이언트단 GET Parameter

    Date2019.11.16 Category코드 ByHanam09 Views467
    Read More
  16. JavaScript에서 파이썬 문자열 처리 함수 중 하나 (바인딩)를 구현

    Date2020.01.20 Category코드 BySeia Views464
    Read More
  17. Koa에서 자동으로 라우팅 채워주기

    Date2020.01.22 Category코드 BySeia Views438
    Read More
  18. C언어 삼중자를 이용한 코드

    Date2018.07.22 Category코드 Bytitle: 황금 서버 (30일)humit Views410
    Read More
  19. 내가 만든 함수 모음집

    Date2018.05.08 Category코드 Bytitle: 대한민국 국기gimmepoint Views382
    Read More
  20. 미완성 받아쓰기 (C)

    Date2018.04.20 Category코드 Bytitle: 대한민국 국기gimmepoint Views380
    Read More
Board Pagination Prev 1 2 3 4 Next
/ 4