Files
nuxt4-deep/server/api/04-users/[id].get.ts
hyeonggil fc7d3d14cf Add initial Nuxt 4 project setup with essential configurations
- 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.
2026-04-08 23:59:29 +09:00

35 lines
962 B
TypeScript

/**
* 케이스 4: 동적 라우트 파라미터
*
* - 파일명의 [id] → URL의 동적 세그먼트
* - URL: GET /api/04-users/123
* - getRouterParam(event, 'id'): 단일 파라미터 읽기
* - getRouterParams(event): 전체 파라미터 객체 반환
*
* 중첩 동적 라우트 예시:
* server/api/04-users/[userId]/posts/[postId].get.ts
* → GET /api/04-users/1/posts/42
*/
const USERS: Record<string, { id: string; name: string; role: string }> = {
"1": { id: "1", name: "김철수", role: "admin" },
"2": { id: "2", name: "이영희", role: "user" },
"3": { id: "3", name: "박민준", role: "user" },
};
export default defineEventHandler((event) => {
// URL 파라미터 읽기
const id = getRouterParam(event, "id");
const user = USERS[id ?? ""];
if (!user) {
throw createError({
statusCode: 404,
statusMessage: `ID ${id}에 해당하는 사용자가 없습니다.`,
});
}
return user;
});