- 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.
37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
/**
|
|
* 케이스 7: server/utils/ 헬퍼 활용
|
|
*
|
|
* - URL: GET /api/07-products
|
|
* - server/utils/response.ts의 apiSuccess()를 import 없이 사용 (auto-import)
|
|
* - getHeader(event, key): 특정 요청 헤더 값 읽기
|
|
* - setHeader(event, key, value): 응답 헤더 설정
|
|
*
|
|
* 테스트:
|
|
* fetch('/api/07-products', {
|
|
* headers: { 'Authorization': 'Bearer my-token' }
|
|
* })
|
|
*/
|
|
|
|
const PRODUCTS = [
|
|
{ id: 1, name: "Nuxt 4 전자책", price: 29000 },
|
|
{ id: 2, name: "Vue 3 강의", price: 49000 },
|
|
];
|
|
|
|
export default defineEventHandler((event) => {
|
|
// 요청 헤더 읽기
|
|
const token = extractBearerToken(event); // server/utils/response.ts에서 auto-import
|
|
|
|
// 응답 헤더 설정
|
|
setHeader(event, "X-Total-Count", String(PRODUCTS.length));
|
|
setHeader(event, "Cache-Control", "max-age=60");
|
|
|
|
// 인증 정보 포함 여부에 따라 응답 달리
|
|
if (token) {
|
|
return apiSuccess(PRODUCTS, { authenticated: true, token: token.slice(0, 6) + "***" });
|
|
}
|
|
|
|
// 공개 응답 (가격 숨김)
|
|
const publicProducts = PRODUCTS.map(({ id, name }) => ({ id, name }));
|
|
return apiSuccess(publicProducts, { authenticated: false });
|
|
});
|