- Created .gitignore to exclude build outputs, logs, and environment files. - Added nuxt.config.ts for project configuration, enabling Tailwind CSS and Pinia modules. - Initialized package.json with scripts for development and production, and added necessary dependencies. - Generated pnpm-lock.yaml for dependency management. - Created README.md with setup instructions and development guidelines. - Implemented server API examples in server/api/ directory, demonstrating various use cases. - Added middleware for logging requests and responses. - Included example Vue components and pages for server API interaction. - Established basic project structure for Nuxt 4 application development.
38 lines
990 B
TypeScript
38 lines
990 B
TypeScript
/**
|
|
* 케이스 2: POST 요청 + readBody
|
|
*
|
|
* - 파일명에 .post.ts 접미사 → POST 요청만 처리
|
|
* - URL: POST /api/02-users
|
|
* - readBody(event): 요청 본문(JSON)을 파싱해서 반환
|
|
* - setResponseStatus(): 응답 상태 코드 설정 (기본값 200)
|
|
*/
|
|
|
|
interface CreateUserBody {
|
|
name: string;
|
|
email: string;
|
|
}
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
// 요청 본문 파싱 (자동으로 JSON → 객체 변환)
|
|
const body = await readBody<CreateUserBody>(event);
|
|
|
|
// 간단한 유효성 검사
|
|
if (!body.name || !body.email) {
|
|
// createError: HTTP 에러 응답 생성 (케이스 6에서 자세히 다룸)
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: "name과 email은 필수입니다.",
|
|
});
|
|
}
|
|
|
|
// 201 Created 상태 코드로 응답
|
|
setResponseStatus(event, 201);
|
|
|
|
return {
|
|
id: crypto.randomUUID(),
|
|
name: body.name,
|
|
email: body.email,
|
|
createdAt: new Date().toISOString(),
|
|
};
|
|
});
|