조회 수 436 추천 수 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 Views164
    Read More
  2. [Python-Gnuboard] 파이썬으로 구현한 그누보드 자동 글쓰기 함수

    Date2021.04.08 Category코드 By이니스프리 Views1221
    Read More
  3. [Python] 휴일지킴이 약국을 크롤링하여 Folium 지도에 마커로 표시하는 PyQt 윈도우 앱

    Date2021.03.13 Category코드 By이니스프리 Views1143
    Read More
  4. 도박 중독자를 위한 광고 차단 규칙

    Date2020.08.21 Category코드 By제르엘 Views292
    Read More
  5. [Python] 유튜브 영상을 다운받아 일정 간격으로 캡쳐하여 10장씩 merge하기

    Date2020.05.27 Category코드 By이니스프리 Views1050
    Read More
  6. [Autohotkey] 매분 정각에 전체화면을 캡쳐하는 스크립트

    Date2020.05.22 Category자료 By이니스프리 Views1102
    Read More
  7. [Python/Telegram] Studyforus 알림봇 (댓글, 스티커 파싱)

    Date2020.05.15 Category코드 By이니스프리 Views648
    Read More
  8. [Python] url 주소로부터 IP 주소 알아내기

    Date2020.02.20 Category코드 Bytitle: 황금 서버 (30일)humit Views2060
    Read More
  9. [Python] 네이버 실시간 검색어

    Date2020.01.23 Category코드 Bytitle: 황금 서버 (30일)humit Views1123
    Read More
  10. Koa에서 자동으로 라우팅 채워주기

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

    Date2020.01.20 Category코드 BySeia Views462
    Read More
  12. [Python] Google Image Search 결과를 받아오기

    Date2019.12.09 Category코드 By이니스프리 Views947
    Read More
  13. [파이썬] Requests를 사용한 네이버 카페 크롤링 - 일정수 이상의 리플이 달린 게시글만 텔레그램 알림

    Date2019.11.17 Category코드 By이니스프리 Views4187
    Read More
  14. [JS] 클라이언트단 GET Parameter

    Date2019.11.16 Category코드 ByHanam09 Views465
    Read More
  15. [Python] 싸이월드 미니홈피 백업 스크립트

    Date2019.11.07 Category코드 By이니스프리 Views2165
    Read More
  16. [Python] PIL을 이용한 Animated GIF의 리사이징

    Date2019.11.03 Category코드 By이니스프리 Views1101
    Read More
  17. [PyQt] sir.kr에서 스크랩한 게시글을 보여주는 윈도우앱 (검색 및 정렬 가능)

    Date2019.08.09 Category코드 By이니스프리 Views998
    Read More
  18. [아미나] Dropbox API를 이용한 이미지 호스팅 보드스킨

    Date2019.07.13 Category코드 By이니스프리 Views1374
    Read More
  19. [Python] 네이버 모바일 이미지 검색에서의 이미지 파일을 멀티스레드로 다운받고 1개의 파일로 병합

    Date2019.07.12 Category코드 By이니스프리 Views1376
    Read More
  20. [PHP/Javascript] 아미나에 자동으로 게시글을 생성하고 Ajax로 전송하여 결과를 표시하기

    Date2019.07.09 Category코드 By이니스프리 Views775
    Read More
Board Pagination Prev 1 2 3 4 Next
/ 4