연락처서비스 v2.0

(node.js + express + mongodb)

  • 간단한 RESTful Service 예제입니다. 내용은 사이드 메뉴를 열어서 확인하세요.
  • 연락처 데이터는 매일 새벽2시에 초기화되어 100건만 남습니다.
  • JSONP 지원(O), CORS 지원(O)
  • node.js + express + mongodb + mongoose + es6

: 연락처 목록 조회 기능. 페이지 번호가 0인 경우 전체 데이터 조회

- 전달 파라미터
  • pageno : 페이지 번호(기본값:0)
  • pagesize : 페이지 사이즈(기본값:5)
- 요청 형식
- 응답데이터 형식
{
    "pageno": 1,
    "pagesize": 2,
    "totalcount": 103,
    "contacts": [
        {
            "no": "5c7129de4840822eb4d2c26a",
            "name": "트럼프",
            "tel": "010-2222-3333",
            "address": "서울시",
            "photo": "http://localhost:3000/photos/LUgMVPm0Q"
        },
        {
            "no": "5c7129de4840822eb4d2c269",
            "name": "샌더스",
            "tel": "010-2222-3332",
            "address": "서울시",
            "photo": "http://localhost:3000/photos/nOF1rSRft"
        }
    ]
}

: 특정 연락처 한건 조회

- 전달 파라미터
  • :no : 연락처 고유 번호(Key)
- 요청 형식
- 응답데이터 형식
{
    "name": "샌더스",
    "tel": "010-2222-3332",
    "address": "서울시",
    "photo": "http://localhost:3000/photos/nOF1rSRft",
    "no": "5c7129de4840822eb4d2c269"
}
- 응답데이터 형식(실패)
{
    "status": "fail",
    "message": "연락처 정보 없음"
}

: 이름을 이용해 연락처 검색

- 전달 파라미터
  • :name : 이름의 일부 문자열(2글자 이상만 허용함)
- 요청 형식
- 응답데이터 형식
[
    {
        "no": "5c7129c600c812065c3964e0",
        "name": "Rachel Johnson",
        "tel": "010-3456-8225",
        "address": "서울시",
        "photo": "http://localhost:3000/photos/YKPoqQqkkKu"
    },
    {
        "no": "5c7129c600c812065c396506",
        "name": "Rosebud Jones",
        "tel": "010-3456-8263",
        "address": "서울시",
        "photo": "http://localhost:3000/photos/4wXGJU4i1G"
    }
]

: 새로운 연락처 추가(초기사진은 noimage.jpg로 설정함)

: 사진을 수정하려면 POST /contacts/:no/photo API를 호출해야 함.

- 전달 파라미터
  • name : 이름
  • tel : 전화번호
  • address : 주소
- 요청 형식(conent-type:application/json 인 경우)
{
    "name":"강감찬",
    "tel":"010-2222-3339",
    "address":"서울시"
}
- 요청 형식(conent-type:application/x-www-form-urlencoded 인 경우)
name=....&tel=....&address=....
- 응답데이터 형식(성공)
{
    "status": "ok",
    "message": "연락처 추가 성공",
    "no": "5c713b495489753ce808e673"
}
- 응답데이터 형식(실패)
{
   "status": "fail",
   "message": "[에러메시지]"
}

: 기존 연락처 정보 변경

- 전달 파라미터
  • :no : 연락처 고유 번호(Key)
  • name : 이름
  • tel : 전화번호
  • address : 주소
- 요청 형식(conent-type:application/json 인 경우)
PUT /contacts/5c7144886484aa140412f884
{
    "name":"강감찬",
    "tel":"010-2222-3339",
    "address":"서울시"
}
- 요청 형식(conent-type:application/x-www-form-urlencoded 인 경우)
no=...&name=....&tel=....&address=....
- 응답데이터 형식(성공)
{
    "status": "success",
    "message": "업데이트 성공",
    "no": "5c7144886484aa140412f884"
}
- 응답데이터 형식(실패)
{
   "status": "fail",
   "message": "[에러메시지]"
}

: 기존 연락처 삭제

- 전달 파라미터
  • :no : 연락처 고유 번호(Key)
- 요청 형식
  • /contacts/1491350298389
- 응답데이터 형식(성공)
{
   "status": "success",
   "message": "삭제 성공",
   "no" : "5c7144886484aa140412f884"
}
- 응답데이터 형식(실패)
{
   "status": "fail",
   "message": "[에러메시지]"
}

: 여러 건의 연락처를 한번에 추가함

- 전달 파라미터
- 요청 형식(conent-type:application/json 만 지원)
[ 
   { "name":"오바마", "tel":"010-2222-3339", "address":"서울시" }, 
   { "name":"샌더스", "tel":"010-2222-3332", "address":"서울시" },
   { "name":"트럼프", "tel":"010-2222-3333", "address":"서울시" }
]
- 응답데이터 형식(성공)
{
    "status": "ok",
    "message": "3건의 데이터 추가 성공",
    "no": [
        "5c7144886484aa140412f882",
        "5c7144886484aa140412f883",
        "5c7144886484aa140412f884"
    ]
}
- 응답데이터 형식(실패)
{
   "status": "fail",
   "message": "[에러메시지]"
}

: 연락처 정보의 사진을 업로드하고 변경함

- 전달 파라미터
  • photo 파일 필드 : multipart/form-data
- 요청 형식 폼(conent-type:multipart/form-data 만 지원)
<form method="post" enctype="multipart/form-data" action="/contacts/{no}/photo"> 
    <input type="file" name="photo"> 
    <input type="submit"> 
</form> 
- 응답데이터 형식(성공)
{
    "status": "success",
    "message": "사진 변경 성공 => photo_id : ihAmNA6fM",
    "no": "5c7144886484aa140412f884"
}
- 응답데이터 형식(실패)
{
    "status": "fail",
    "message": "[에러메시지]"
}
- AJAX 요청 예제(axios 사용)
//자세한 예제는 https://github.com/mzabriskie/axios/blob/master/examples/upload/index.html 참조
import axios from 'axios';

var data = new FormData();
data.append('photo', document.getElementById('photo').files[0]);

axios.post('/api/contacts/' + no + '/photo', data)
.then((response) => {
    console.log(response);
})
.catch((ex) => {
    console.log('updatePhoto failed', ex);
});

: 1초의 지연시간 후 연락처 목록 응답. 페이지 번호가 0인 경우 전체 데이터 조회

- 전달 파라미터
  • pageno : 페이지 번호(기본값:0)
  • pagesize : 페이지 사이즈(기본값:5)
- 요청 형식
- 응답데이터 형식
{
    "pageno": 1,
    "pagesize": 2,
    "totalcount": 106,
    "contacts": [
        {
            "no": "5c7144886484aa140412f884",
            "name": "모모",
            "tel": "010-9999-9999",
            "address": "제주",
            "photo": "http://localhost:3000/photos/kVUsltfiG"
        },
        {
            "no": "5c7144886484aa140412f883",
            "name": "정연",
            "tel": "010-2222-3332",
            "address": "서울시",
            "photo": "http://localhost:3000/photos/noimage"
        }
    ]
}

: 이름을 이용해 연락처 검색(1초의 지연시간 발생)

- 전달 파라미터
  • :name : 이름의 일부 문자열(2글자 이상만 허용함)
- 요청 형식
- 응답데이터 형식
[
    {
        "no": "5c7129c600c812065c3964e0",
        "name": "Rachel Johnson",
        "tel": "010-3456-8225",
        "address": "서울시",
        "photo": "http://localhost:3000/photos/YKPoqQqkkKu"
    },
    {
        "no": "5c7129c600c812065c396506",
        "name": "Rosebud Jones",
        "tel": "010-3456-8263",
        "address": "서울시",
        "photo": "http://localhost:3000/photos/4wXGJU4i1G"
    }
]