JSON Server 활용법

상현·2023년 11월 28일

JSON Server는 당장 back-end를 직접 구축하기 어려울 때, MOCK 서버로 활용할 수 있는 가상의 서버를 구축하는 기능을 제공한다.

앞으로 프로젝트에서 많이 쓰이게 될 것 같아 유용하게 쓸 수 있는 기능들을 정리하고자 한다.

설치 및 실행

설치

npm install -g json-server

yarn add json-server

실행

json-server --watch db.json --port [port번호]

사용법

서버를 실행하고 나면 지정한 포트로 서버가 열린다. 나 같은 경우는 4000번 포트로 열었고, localhost:4000으로 들어가 보면 어떤 데이터가 들어 있는지 확인할 수 있다.

아래에는 앞으로 유용하게 쓰일 수 있을 것 같은 쿼리들을 정리했다.

Test용 db.json

{
  "posts": [
    {
      "title": "제목1",
      "content": "내용1",
      "author": "글쓴이1",
      "vote": {
        "like": 5,
        "dislike": 0
      },
      "id": "1"
    },
    {
      "title": "제목2",
      "content": "내용2ㅋㅋ",
      "author": "글쓴이2",
      "vote": {
        "like": 0,
        "dislike": -3
      },
      "id": "2"
    },
    {
      "title": "제목3",
      "content": "내용3",
      "author": "글쓴이3",
      "vote": {
        "like": 0,
        "dislike": 0
      },
      "id": "3"
    },
    {
      "title": "제목4",
      "content": "내용4",
      "author": "글쓴이4",
      "vote": {
        "like": 6,
        "dislike": 0
      },
      "id": "4"
    },
    {
      "title": "제목5",
      "content": "내용5",
      "author": "글쓴이5",
      "vote": {
        "like": 0,
        "dislike": 0
      },
      "id": "5"
    },
    {
      "title": "제목6",
      "content": "내용6",
      "author": "글쓴이6",
      "vote": {
        "like": 0,
        "dislike": 0
      },
      "id": "6"
    },
    {
      "title": "제목7",
      "content": "내용7ㅋㅋ",
      "author": "글쓴이7",
      "vote": {
        "like": 0,
        "dislike": 0
      },
      "id": "7"
    },
    {
      "title": "제목8",
      "content": "내용8",
      "author": "글쓴이8",
      "vote": {
        "like": 0,
        "dislike": 1
      },
      "id": "8"
    },
    {
      "title": "제목9",
      "content": "내용9",
      "author": "글쓴이9",
      "vote": {
        "like": 7,
        "dislike": 0
      },
      "id": "9"
    }
  ],
  "comments": [
    {
      "postId": "1",
      "content": "내용1",
      "author": "글쓴이1",
      "id": "1"
    },
    {
      "postId": "2",
      "content": "내용2",
      "author": "글쓴이2",
      "id": "2"
    },
    {
      "postId": "3",
      "content": "내용3",
      "author": "글쓴이3",
      "id": "3"
    },
    {
      "postId": "4",
      "content": "내용4",
      "author": "글쓴이4",
      "id": "4"
    },
    {
      "postId": "5",
      "content": "내용5",
      "author": "글쓴이5",
      "id": "5"
    },
    {
      "postId": "6",
      "content": "내용6",
      "author": "글쓴이6",
      "id": "6"
    },
    {
      "postId": "7",
      "content": "내용7",
      "author": "글쓴이7",
      "id": "7"
    }
  ]
}

posts/findAll

[
    {
        "title": "제목1",
        "content": "내용1",
        "author": "글쓴이1",
        "vote": {
            "like": 5,
            "dislike": 0
        },
        "id": "1"
    },
    {
        "title": "제목2",
        "content": "내용2ㅋㅋ",
        "author": "글쓴이2",
        "vote": {
            "like": 0,
            "dislike": -3
        },
        "id": "2"
    },
    {
        "title": "제목3",
        "content": "내용3",
        "author": "글쓴이3",
        "vote": {
            "like": 0,
            "dislike": 0
        },
        "id": "3"
    },
    {
        "title": "제목4",
        "content": "내용4",
        "author": "글쓴이4",
        "vote": {
            "like": 6,
            "dislike": 0
        },
        "id": "4"
    },
    {
        "title": "제목5",
        "content": "내용5",
        "author": "글쓴이5",
        "vote": {
            "like": 0,
            "dislike": 0
        },
        "id": "5"
    },
    {
        "title": "제목6",
        "content": "내용6",
        "author": "글쓴이6",
        "vote": {
            "like": 0,
            "dislike": 0
        },
        "id": "6"
    },
    {
        "title": "제목7",
        "content": "내용7ㅋㅋ",
        "author": "글쓴이7",
        "vote": {
            "like": 0,
            "dislike": 0
        },
        "id": "7"
    },
    {
        "title": "제목8",
        "content": "내용8",
        "author": "글쓴이8",
        "vote": {
            "like": 0,
            "dislike": 1
        },
        "id": "8"
    },
    {
        "title": "제목9",
        "content": "내용9",
        "author": "글쓴이9",
        "vote": {
            "like": 7,
            "dislike": 0
        },
        "id": "9"
    }
]

posts/findById

{
    "title": "제목1",
    "content": "내용1",
    "author": "글쓴이1",
    "vote": {
        "like": 5,
        "dislike": 0
    },
    "id": "1"
}

posts/findByTitle

[
    {
        "title": "제목1",
        "content": "내용1",
        "author": "글쓴이1",
        "vote": {
            "like": 5,
            "dislike": 0
        },
        "id": "1"
    }
]

posts/findByLikeGreaterThan

[
    {
        "title": "제목1",
        "content": "내용1",
        "author": "글쓴이1",
        "vote": {
            "like": 5,
            "dislike": 0
        },
        "id": "1"
    },
    {
        "title": "제목4",
        "content": "내용4",
        "author": "글쓴이4",
        "vote": {
            "like": 6,
            "dislike": 0
        },
        "id": "4"
    },
    {
        "title": "제목9",
        "content": "내용9",
        "author": "글쓴이9",
        "vote": {
            "like": 7,
            "dislike": 0
        },
        "id": "9"
    }
]

posts/findByContentLike

[
    {
        "title": "제목2",
        "content": "내용2ㅋㅋ",
        "author": "글쓴이2",
        "vote": {
            "like": 0,
            "dislike": -3
        },
        "id": "2"
    },
    {
        "title": "제목7",
        "content": "내용7ㅋㅋ",
        "author": "글쓴이7",
        "vote": {
            "like": 0,
            "dislike": 0
        },
        "id": "7"
    }
]

posts/findByTitleExclude

[
    {
        "title": "제목1",
        "content": "내용1",
        "author": "글쓴이1",
        "vote": {
            "like": 5,
            "dislike": 0
        },
        "id": "1"
    },
    {
        "title": "제목2",
        "content": "내용2ㅋㅋ",
        "author": "글쓴이2",
        "vote": {
            "like": 0,
            "dislike": -3
        },
        "id": "2"
    },
    {
        "title": "제목3",
        "content": "내용3",
        "author": "글쓴이3",
        "vote": {
            "like": 0,
            "dislike": 0
        },
        "id": "3"
    },
    {
        "title": "제목4",
        "content": "내용4",
        "author": "글쓴이4",
        "vote": {
            "like": 6,
            "dislike": 0
        },
        "id": "4"
    },
    {
        "title": "제목5",
        "content": "내용5",
        "author": "글쓴이5",
        "vote": {
            "like": 0,
            "dislike": 0
        },
        "id": "5"
    },
    {
        "title": "제목6",
        "content": "내용6",
        "author": "글쓴이6",
        "vote": {
            "like": 0,
            "dislike": 0
        },
        "id": "6"
    },
    {
        "title": "제목7",
        "content": "내용7ㅋㅋ",
        "author": "글쓴이7",
        "vote": {
            "like": 0,
            "dislike": 0
        },
        "id": "7"
    },
    {
        "title": "제목8",
        "content": "내용8",
        "author": "글쓴이8",
        "vote": {
            "like": 0,
            "dislike": 1
        },
        "id": "8"
    },
    {
        "title": "제목9",
        "content": "내용9",
        "author": "글쓴이9",
        "vote": {
            "like": 7,
            "dislike": 0
        },
        "id": "9"
    }
]

posts/findAllOrderByLikeDesc

[
    {
        "title": "제목9",
        "content": "내용9",
        "author": "글쓴이9",
        "vote": {
            "like": 7,
            "dislike": 0
        },
        "id": "9"
    },
    {
        "title": "제목4",
        "content": "내용4",
        "author": "글쓴이4",
        "vote": {
            "like": 6,
            "dislike": 0
        },
        "id": "4"
    },
    {
        "title": "제목1",
        "content": "내용1",
        "author": "글쓴이1",
        "vote": {
            "like": 5,
            "dislike": 0
        },
        "id": "1"
    },
    {
        "title": "제목2",
        "content": "내용2ㅋㅋ",
        "author": "글쓴이2",
        "vote": {
            "like": 0,
            "dislike": -3
        },
        "id": "2"
    },
    {
        "title": "제목3",
        "content": "내용3",
        "author": "글쓴이3",
        "vote": {
            "like": 0,
            "dislike": 0
        },
        "id": "3"
    },
    {
        "title": "제목5",
        "content": "내용5",
        "author": "글쓴이5",
        "vote": {
            "like": 0,
            "dislike": 0
        },
        "id": "5"
    },
    {
        "title": "제목6",
        "content": "내용6",
        "author": "글쓴이6",
        "vote": {
            "like": 0,
            "dislike": 0
        },
        "id": "6"
    },
    {
        "title": "제목7",
        "content": "내용7ㅋㅋ",
        "author": "글쓴이7",
        "vote": {
            "like": 0,
            "dislike": 0
        },
        "id": "7"
    },
    {
        "title": "제목8",
        "content": "내용8",
        "author": "글쓴이8",
        "vote": {
            "like": 0,
            "dislike": 1
        },
        "id": "8"
    }
]

comments/findPostContainingComments

{
    "title": "제목1",
    "content": "내용1",
    "author": "글쓴이1",
    "vote": {
        "like": 5,
        "dislike": 0
    },
    "id": "1",
    "comments": [
        {
            "postId": "1",
            "content": "내용1",
            "author": "글쓴이1",
            "id": "1"
        }
    ]
}

comments/findCommentContainingPost

  • 요청 주소: http://{{DOMAIN}}:{{PORT}}/comments/{{id}}?_expand=post
  • 기능: comments에서 id로 comment를 찾고 그 comment가 부모로하는 post를 출력한다.
  • 주의: expand=posts가 아니라 expand=post인 것에 유의해야 한다!! (1:1 이기 때문)
  • 실행 결과
{
    "postId": "1",
    "content": "내용1",
    "author": "글쓴이1",
    "id": "1",
    "post": {
        "title": "제목1",
        "content": "내용1",
        "author": "글쓴이1",
        "vote": {
            "like": 5,
            "dislike": 0
        },
        "id": "1"
    }
}
profile
블로그 이전 => https://shdev.blog/

0개의 댓글