조회 수 437 추천 수 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. AdBlock 접근 방지 애드온 v0.1

    Date2017.10.05 Category자료 By네모 Views649
    Read More
  2. AdminLTE용 에디터 스타일

    Date2017.07.07 Category자료 Bytitle: 은메달도다 Views770
    Read More
  3. AWSCLI, in a single file (portable, linux)

    Date2021.04.10 Category코드 BySeia Views165
    Read More
  4. c 이진트리 전,중,후위 알고리즘

    Date2018.04.24 Category코드 Bytitle: 대한민국 국기gimmepoint Views495
    Read More
  5. Cmd 에서 서비스 시작 / 종료하기

    Date2018.02.18 Category코드 ByProjectSE Views644
    Read More
  6. CMD로 로컬 연결 고정 IP 설정하기

    Date2018.02.06 Category코드 Bytitle: 황금 서버 (30일)humit Views1038
    Read More
  7. C언어 삼중자를 이용한 코드

    Date2018.07.22 Category코드 Bytitle: 황금 서버 (30일)humit Views410
    Read More
  8. even_move - 감성적인 에러 페이지

    Date2017.08.08 Category자료 Bytitle: 열려라 맛스타의 자물쇠TVJ Views798
    Read More
  9. Gentelella

    Date2017.06.29 Category자료 ByNoYeah Views805
    Read More
  10. Gentelella 레이아웃에 사용가능한 가격 테이블 위젯입니다.

    Date2017.07.03 Category자료 ByNoYeah Views638
    Read More
  11. Git 저장소에서 자동으로 받아 업데이트하는 쉘 스크립트

    Date2017.09.16 Category코드 ByNoYeah Views651
    Read More
  12. Hello, World!를 출력해보자

    Date2018.04.21 Category코드 By네모 Views568
    Read More
  13. HEX를 RGB로, RGB를 HEX로 바꾸는 PHP 코드

    Date2018.05.05 Category코드 By네모 Views507
    Read More
  14. html 초보가 만든 자소서

    Date2018.04.21 Category코드 Bytitle: 대한민국 국기gimmepoint Views651
    Read More
  15. JavaScript에서 파이썬 문자열 처리 함수 중 하나 (바인딩)를 구현

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

    Date2020.01.22 Category코드 BySeia Views437
    Read More
  17. RBGE - 이쁘고 깔끔한 에러페이지

    Date2017.08.08 Category자료 Bytitle: 열려라 맛스타의 자물쇠TVJ Views687
    Read More
  18. [1.8a] Bootstrap 'Panel' 위젯 스타일

    Date2017.08.09 Category자료 Bytitle: 은메달도다 Views602
    Read More
  19. [Autohotkey] 매분 정각에 전체화면을 캡쳐하는 스크립트

    Date2020.05.22 Category자료 By이니스프리 Views1103
    Read More
  20. [Bootstrap] xeACE 레이아웃

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