๋ฐ˜์‘ํ˜•

๐ŸŽˆ JSON Server ์‚ฌ์šฉํ•˜๊ธฐ

1. REST / REST API / Json Server

  • REST: ์›น์˜ ๊ธฐ๋ณธ ํ”„๋กœํ† ์ฝœ์ธ HTTP ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„๋˜๋ฉฐ , HTTP ๋ฉ”์„œ๋“œ์™€ URL ์„ ์ด์šฉํ•˜์—ฌ ์ž์›๊ณผ ํ•˜๊ณ ์žํ•˜๋Š” ํ–‰๋™์„ ํ‘œํ˜„ํ•จ.
  • REST API: REST ํ•œ ๋ฐฉ์‹์œผ๋กœ ์„ค๊ณ„๋œ API.
  • Json Server: JSON ํŒŒ์ผ์„ ์ด์šฉํ•˜์—ฌ Rest API ์„œ๋ฒ„๋ฅผ ๋น ๋ฅด๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ์„ฑ ํ•˜๊ธฐ ์œ„ํ•œ ๋„๊ตฌ. 
    ex) (GET) http://example.com/wedding/1 : 1๋ฒˆ id ์ฒญ์ฒฉ์žฅ ๊ฐ€์ ธ์˜ค๊ธฐ

2. JSON Server ์‚ฌ์šฉํ•˜๊ธฐ

https://github.com/typicode/json-server

01. JSON Server ์„ค์น˜

yarn add -D json-server
  • ๋ฃจํŠธ๊ฒฝ๋กœ์— db.json ์ƒ์„ฑ
{
  "posts": [
    { "id": "1", "title": "a title", "views": 100 },
    { "id": "2", "title": "another title", "views": 200 },
    { "id": "3", "title": "json-server", "views": 200 }

  ],
  "comments": [
    { "id": "1", "text": "a comment about post 1", "postId": "1" },
    { "id": "2", "text": "another comment about post 1", "postId": "1" }
  ],
  "profile": {
    "name": "typicode"
  }
}
  • package.json์— ์•„๋ž˜ ๋‚ด์šฉ ์ถ”๊ฐ€
"dev:db": "json-server --watch db.json --port=8888" 
// watch์˜ต์…˜์„ ์ฃผ๋ฉด db.json์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๊ฐœ๋ฐœ์„œ๋ฒ„์— ์‹ค์‹œ๊ฐ„ ๋ฐ˜์˜ํ•ด์ค€๋‹ค.
  • ์‹คํ–‰
yarn dev:db

json server ์„ค์น˜์™„๋ฃŒ!

 

02. ๋™์ž‘ test (https://github.com/typicode/json-server?tab=readme-ov-file)

  • Filter
  • ์‹คํ–‰๊ฒฐ๊ณผ 
    json server filter ์‹คํ–‰๊ฒฐ๊ณผ

  • Paginate
    Use _page and optionally _limit to paginate returned data. In the Link header you'll get firstprevnext and last links.
    GET /posts?_page=7   
    โ€‹
    • _page=7 : ์ผ๊ณฑ ๋ฒˆ์งธ ํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•œ๋‹ค.
    • _limit ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋ช…์‹œ๋˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ, json-server๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํŽ˜์ด์ง€๋‹น 10๊ฐœ์˜ ํ•ญ๋ชฉ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    • ๊ฒฐ๊ณผ : ๊ธฐ๋ณธ ํŽ˜์ด์ง€๋‹น 10๊ฐœ ํ•ญ๋ชฉ์„ ์‚ฌ์šฉํ•ด 61~70๋ฒˆ์งธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    GET /posts?_page=7&_limit=20
    
    • _page=7: _page=7์œผ๋กœ ์ผ๊ณฑ ๋ฒˆ์งธ ํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•œ๋‹ค.
    • _limit: _limit=20์„ ์ง€์ •ํ•˜์—ฌ ํŽ˜์ด์ง€๋‹น 20๊ฐœ์˜ ํ•ญ๋ชฉ์„ ๊ฐ€์ ธ์˜จ๋‹ค.
    • ์ผ๊ณฑ ๋ฒˆ์งธ ํŽ˜์ด์ง€์—์„œ 20๊ฐœ์˜ ํ•ญ๋ชฉ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ, posts ๋ฐ์ดํ„ฐ ์ค‘ 121๋ฒˆ์งธ์—์„œ 140๋ฒˆ์งธ ํ•ญ๋ชฉ๊นŒ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 
  • Sort
    _sort=f1,f2
    ๋ฐ์ดํ„ฐ๋ฅผ ํŠน์ • ํ•„๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ •๋ ฌํ•  ๋•Œ๋Š” ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ _sort๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์ •๋ ฌ ์ˆœ์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์ด๋ฉฐ, ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์„ค์ •ํ•˜๋ ค๋ฉด ํ•„๋“œ ์•ž์— -๋ฅผ ๋ถ™์ธ๋‹ค.
    GET /posts?_sort=id,-views
    1. GET /posts?_sort=id
    • id ํ•„๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
    • posts ๋ฐ์ดํ„ฐ๊ฐ€ id ๊ฐ’์ด ์ž‘์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋˜์–ด ๋ฐ˜ํ™˜ ๋œ๋‹ค.
    2. GET /posts?_sort=-views
    • views ํ•„๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
    • posts ๋ฐ์ดํ„ฐ๊ฐ€ views ๊ฐ’์ด ํฐ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋˜์–ด ๋ฐ˜ํ™˜ ๋œ๋‹ค.
    3. ๋‹ค์ค‘ ์ •๋ ฌ ์˜ˆ์‹œ: GET /posts?_sort=id,-views
    • ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ•„๋“œ๋ฅผ ์ •๋ ฌ ๊ธฐ์ค€์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, id ํ•„๋“œ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋จผ์ € ์ •๋ ฌํ•œ ๋‹ค์Œ, ๋™์ผํ•œ id ๊ฐ’์ด ์žˆ์„ ๊ฒฝ์šฐ views ํ•„๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.
    • ์šฐ์„  id ๊ฐ’์ด ์ž‘์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋˜๋ฉฐ, ๊ฐ™์€ id ๊ฐ’์„ ๊ฐ€์ง„ ํ•ญ๋ชฉ๋“ค์€ views ๊ฐ’์ด ํฐ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋œ๋‹ค.

 

 

 

๋ฐ˜์‘ํ˜•

+ Recent posts