입력:23/02/23수정:24/01/14

middy route 용례

yarn add @middy/http-router @middy/http-header-normalizer

import httpHeaderNormalizer from '@middy/http-header-normalizer'
import httpRouterHandler, { Method } from '@middy/http-router'
import middy from '@middy/core'

const routes = [
  /* investor */
  {
    method: 'GET',
    path: '/endpoint/user',
    handler: middy().use().handler(getDomesticListApiHandler),
  },
  /* stage별로 문기하기 */
  {
    method: 'GET',
    path: '/{stage}/endpoint/domestic',
    handler: middy().use().handler(getDomesticListApiHandler),
  },
]

export const handler = middy()
  .use(httpHeaderNormalizer())
  .handler(httpRouterHandler(routes))
  .onError((request) => {
    //매칭 안될 경우에 대비해서 에러 리턴
    if (request?.error?.message == 'Route does not exist') {
      request.response = {
        statusCode: 400,
        body: 'No Matching Resource',
        headers: {
          'Content-Type': 'text/plain',
        },
      }
    } else if (request.error && !request.response) {
      // Report unhandled
      request.response = {
        statusCode: 500,
        body: 'An unhandled internal error occurred.',
        headers: {
          'Content-Type': 'text/plain',
        },
      }
    }
  })


토픽: middy
영역: 영역/개발
타입: 타입/스니펫
소스: 도큐먼트