@sumor/api-middleware

api-middleware

Sumor Cloud 도구입니다.
자세한 문서
API Middleware는 Node.JS용 미들웨어입니다.
함수를 API에 쉽게 노출하고 매개변수를 유효성 검사할 수 있습니다.

CI Test Coverage Audit

설치

npm i @sumor/api-middleware --save

전제 조건

Node.JS 버전

Node.JS 버전 16.x 이상이 필요합니다.

Node.JS ES 모듈 필요

이 패키지는 ES 모듈로 작성되었으므로, package.json 파일의 다음 코드를 변경하십시오:

{
  "type": "module"
}

사용 방법

기본 사용법

  1. 프로젝트 폴더 apiplus.js라는 파일 추가
export default async (context, req, res) => {
  const { data } = context
  const { a, b } = data
  return a + b
}

[선택사항] 2. 프로젝트 폴더 apiplus.json이라는 파일 추가

{
  "name": "plus",
  "parameters": {
    "a": {
      "name": "parameter a",
      "type": "number",
      "length": 3
    },
    "b": {
      "name": "parameter b",
      "type": "number"
    }
  }
}
  1. index.js 파일에 다음 코드 추가
import express from 'express'
import apiMiddleware from '@sumor/api-middleware'

const app = express()
apiMiddleware(app, process.cwd() + '/api')

app.listen(3000, () => {
  console.log('서버가 http://localhost:3000에서 실행 중입니다.')
})
  1. index.js 실행
node index.js