조회 수 439 추천 수 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. AWSCLI, in a single file (portable, linux)

    Date2021.04.10 Category코드 BySeia Views167
    Read More
  2. 도박 중독자를 위한 광고 차단 규칙

    Date2020.08.21 Category코드 By제르엘 Views295
    Read More
  3. 내가 만든 merge sort

    Date2018.05.17 Bytitle: 대한민국 국기gimmepoint Views370
    Read More
  4. 내가 만든 함수 모음집 2

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

    Date2018.04.20 Category코드 Bytitle: 대한민국 국기gimmepoint Views380
    Read More
  6. 내가 만든 함수 모음집

    Date2018.05.08 Category코드 Bytitle: 대한민국 국기gimmepoint Views382
    Read More
  7. C언어 삼중자를 이용한 코드

    Date2018.07.22 Category코드 Bytitle: 황금 서버 (30일)humit Views410
    Read More
  8. Koa에서 자동으로 라우팅 채워주기

    Date2020.01.22 Category코드 BySeia Views439
    Read More
  9. JavaScript에서 파이썬 문자열 처리 함수 중 하나 (바인딩)를 구현

    Date2020.01.20 Category코드 BySeia Views465
    Read More
  10. [JS] 클라이언트단 GET Parameter

    Date2019.11.16 Category코드 ByHanam09 Views468
    Read More
  11. 아주 간단한 기초 C++

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

    Date2018.04.24 Category코드 Bytitle: 대한민국 국기gimmepoint Views495
    Read More
  13. 파이선 셸에서 실행하면...?

    Date2018.07.22 Category코드 By제르엘 Views499
    Read More
  14. HEX를 RGB로, RGB를 HEX로 바꾸는 PHP 코드

    Date2018.05.05 Category코드 By네모 Views507
    Read More
  15. 내가 만든 사칙연산 계산기

    Date2018.05.11 Category코드 Bytitle: 대한민국 국기gimmepoint Views515
    Read More
  16. 링크 파싱 애드온용 스킨 (트위터 스타일)

    Date2017.10.03 Category자료 BySNAX Views516
    Read More
  17. [Python] 모 정부기관 사이트 파싱 후 PC 통신처럼 열람하고 싶은 게시글 번호를 입력하면 내용을 보여주는 소스 (허접)

    Date2018.09.14 Category코드 By이니스프리 Views559
    Read More
  18. Hello, World!를 출력해보자

    Date2018.04.21 Category코드 By네모 Views568
    Read More
  19. 매우 특이한 버그

    Date2018.06.05 Category코드 Bytitle: 대한민국 국기gimmepoint Views569
    Read More
  20. [1.8a] Bootstrap 'Panel' 위젯 스타일

    Date2017.08.09 Category자료 Bytitle: 은메달도다 Views602
    Read More
Board Pagination Prev 1 2 3 4 Next
/ 4