apps/web/app/api/check-biz/route.ts의 경로에
Mock API를 작성해보려고 한다.
WHY
위 경로에 파일을 만들면
http://localhost:3000/api/check-biz 으로 요청을 보낼 수 있는 구조가 됨
import { NextRequest, NextResponse } from 'next/server'
export async function POST(req: NextRequest) {
const { bizNumber } = await req.json();
const isValid = typeof bizNumber === 'string' && bizNumber.length === 10 && bizNumber.startsWith('1');
return NextResponse.json({
valid: isValid,
name: isValid ? 'test company' : '등록되지 않은 사업자',
status: isValid ? 'active' : '폐업 또는 미등록',
});
}
POST(req: NextRequest) 함수
Next.js App Router에서 사용하는 API Route 함수
해당 파일 경로로 클라이언트에서 POST 요청을 보낼 수 있게 됨
=> 즉, 클라이언트가 보내는 요청을 처리하는 서버 코드!
await req.json()
클라이언트가 POST 요청으로 bizNumber 데이터를 보내면
서버에서는 데이터 body에서 꺼내서 사용할 수 있어야하는데
await req.json()은 body를 JSON 객체로 변환해주는 함수이고 이걸 통해서 객체 변환을 통해 bizNumber 값을 꺼냄