이 문서는 클라우드 Node.js 학습 프로젝트의 API 사용법을 자세히 설명합니다.
대부분의 API 엔드포인트는 JWT 토큰을 통한 인증이 필요합니다. 인증이 필요한 요청에는 다음 헤더를 포함해야 합니다:
Authorization: Bearer <JWT_TOKEN>
POST /api/auth/register
Content-Type: application/json
{
"name": "홍길동",
"email": "hong@example.com",
"password": "password123"
}응답:
{
"success": true,
"message": "회원가입이 완료되었습니다.",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": "64f8a1b2c3d4e5f6a7b8c9d0",
"name": "홍길동",
"email": "hong@example.com",
"role": "user"
}
}POST /api/auth/login
Content-Type: application/json
{
"email": "hong@example.com",
"password": "password123"
}응답:
{
"success": true,
"message": "로그인되었습니다.",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": "64f8a1b2c3d4e5f6a7b8c9d0",
"name": "홍길동",
"email": "hong@example.com",
"role": "user",
"lastLogin": "2023-09-07T04:30:00.000Z"
}
}GET /api/auth/me
Authorization: Bearer <JWT_TOKEN>응답:
{
"success": true,
"user": {
"id": "64f8a1b2c3d4e5f6a7b8c9d0",
"name": "홍길동",
"email": "hong@example.com",
"role": "user",
"avatar": "",
"isActive": true,
"createdAt": "2023-09-07T04:25:00.000Z",
"updatedAt": "2023-09-07T04:30:00.000Z"
}
}PUT /api/auth/password
Authorization: Bearer <JWT_TOKEN>
Content-Type: application/json
{
"currentPassword": "oldpassword123",
"newPassword": "newpassword123"
}GET /api/users?page=1&limit=10
Authorization: Bearer <ADMIN_JWT_TOKEN>쿼리 파라미터:
page: 페이지 번호 (기본값: 1)limit: 페이지당 항목 수 (기본값: 10)
응답:
{
"success": true,
"count": 10,
"total": 25,
"page": 1,
"pages": 3,
"data": [
{
"id": "64f8a1b2c3d4e5f6a7b8c9d0",
"name": "홍길동",
"email": "hong@example.com",
"role": "user",
"avatar": "",
"isActive": true,
"createdAt": "2023-09-07T04:25:00.000Z"
}
]
}GET /api/users/:id
Authorization: Bearer <JWT_TOKEN>PUT /api/users/:id
Authorization: Bearer <JWT_TOKEN>
Content-Type: application/json
{
"name": "새로운 이름",
"avatar": "https://example.com/avatar.jpg"
}GET /api/users/stats
Authorization: Bearer <ADMIN_JWT_TOKEN>응답:
{
"success": true,
"data": {
"totalUsers": 25,
"newUsersThisMonth": 5,
"adminUsers": 2,
"regularUsers": 23
}
}GET /api/posts?page=1&limit=10&search=검색어&tag=태그명쿼리 파라미터:
page: 페이지 번호 (기본값: 1)limit: 페이지당 항목 수 (기본값: 10)search: 제목 또는 내용에서 검색할 키워드tag: 특정 태그로 필터링
응답:
{
"success": true,
"count": 10,
"total": 50,
"page": 1,
"pages": 5,
"data": [
{
"id": "64f8a1b2c3d4e5f6a7b8c9d1",
"title": "게시글 제목",
"content": "게시글 내용",
"author": {
"id": "64f8a1b2c3d4e5f6a7b8c9d0",
"name": "홍길동",
"email": "hong@example.com",
"avatar": ""
},
"tags": ["태그1", "태그2"],
"image": "https://example.com/image.jpg",
"likes": ["64f8a1b2c3d4e5f6a7b8c9d0"],
"comments": [],
"isPublished": true,
"viewCount": 15,
"likeCount": 1,
"commentCount": 0,
"createdAt": "2023-09-07T04:25:00.000Z",
"updatedAt": "2023-09-07T04:25:00.000Z"
}
]
}GET /api/posts/:idPOST /api/posts
Authorization: Bearer <JWT_TOKEN>
Content-Type: application/json
{
"title": "새로운 게시글",
"content": "게시글 내용입니다.",
"tags": ["Node.js", "Express"],
"image": "https://example.com/image.jpg",
"isPublished": true
}PUT /api/posts/:id
Authorization: Bearer <JWT_TOKEN>
Content-Type: application/json
{
"title": "수정된 제목",
"content": "수정된 내용",
"tags": ["수정된", "태그"],
"isPublished": false
}DELETE /api/posts/:id
Authorization: Bearer <JWT_TOKEN>POST /api/posts/:id/like
Authorization: Bearer <JWT_TOKEN>응답:
{
"success": true,
"message": "좋아요가 추가되었습니다.",
"likeCount": 5
}DELETE /api/posts/:id/like
Authorization: Bearer <JWT_TOKEN>POST /api/posts/:id/comments
Authorization: Bearer <JWT_TOKEN>
Content-Type: application/json
{
"content": "댓글 내용입니다."
}응답:
{
"success": true,
"message": "댓글이 추가되었습니다.",
"data": {
"user": "64f8a1b2c3d4e5f6a7b8c9d0",
"content": "댓글 내용입니다.",
"createdAt": "2023-09-07T04:30:00.000Z"
}
}POST /api/upload
Authorization: Bearer <JWT_TOKEN>
Content-Type: multipart/form-data
image: <파일>응답:
{
"success": true,
"message": "파일이 성공적으로 업로드되었습니다.",
"data": {
"filename": "image-1694062200000-123456789.jpg",
"originalName": "profile.jpg",
"size": 1024000,
"url": "http://localhost:3000/uploads/image-1694062200000-123456789.jpg"
}
}DELETE /api/upload/:filename
Authorization: Bearer <JWT_TOKEN>API는 다음과 같은 에러 응답 형식을 사용합니다:
{
"success": false,
"message": "에러 메시지",
"stack": "에러 스택 (개발 환경에서만)"
}200: 성공201: 생성 성공400: 잘못된 요청401: 인증 실패403: 권한 없음404: 리소스 없음429: 요청 제한 초과500: 서버 오류
- Rate Limiting: API 요청은 15분당 100회로 제한됩니다.
- CORS: 허용된 도메인에서만 API에 접근할 수 있습니다.
- Helmet: 보안 헤더가 자동으로 설정됩니다.
- 입력 검증: 모든 입력 데이터는 Joi를 통해 검증됩니다.
- 비밀번호 암호화: bcrypt를 사용하여 비밀번호가 암호화됩니다.
// 로그인
const login = async (email, password) => {
const response = await fetch('/api/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, password }),
});
const data = await response.json();
return data;
};
// 게시글 생성
const createPost = async (postData, token) => {
const response = await fetch('/api/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
body: JSON.stringify(postData),
});
const data = await response.json();
return data;
};# 로그인
curl -X POST http://localhost:3000/api/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"hong@example.com","password":"password123"}'
# 게시글 조회
curl -X GET http://localhost:3000/api/posts
# 게시글 생성
curl -X POST http://localhost:3000/api/posts \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d '{"title":"제목","content":"내용","isPublished":true}'API 테스트를 위해 Postman 컬렉션을 사용하거나, 위의 예시들을 참고하여 직접 테스트해보세요.
이 가이드가 API 사용에 도움이 되기를 바랍니다. 추가 질문이 있으시면 이슈를 생성해주세요.