git address: https://github.com/yoogukhyeon/react-study-group/tree/api
๐คทโ๏ธ json-server๋?
ํ๋ก ํธ์ํธ ์ฌ์ด๋ํ๋ก์ ํธ ํน์ ๊ฐ๋ฐ์ ํ๋ค๋ณด๋ฉด server ๋ฐ์ดํฐ๊ฐ ํ์ํฉ๋๋ค.
๋ฐฑ์๋ ๊ฐ๋ฐ์ ๋ง์ ์๊ฐ์ ์ฌ์ฉํ ์ ์๋ค๋ฉด json-server๋ฅผ ํ์ฉํด์ API์๋ฒ๋ฅผ ๋ง๋ค์ ์์ต๋๋ค.
์ด๋ฐ ๊ฒฝ์ฐ์ ํ์ํ ๋๊ตฌ๊ฐ json-server ์ ๋๋ค.
์ฐธ๊ณ ๋ก ์ด ๋๊ตฌ๋ ํ๋ก๋์ ์์ ์ฌ์ฉํ๊ธฐ์ํ์ฌ ๋ง๋ค์ด์ง๊ฒ์ ๋๋ค. ์ค์ ์ด์์์๋ ์ฌ์ฉํ์๋ฉด ์๋ฉ๋๋ค.
* ํ๋ก ํธ์๋ ํ ์คํธ ์ฉ๋
1. json-server npm ์ค์น
npm i -D json-server //dev์ ์ค์น
ํ๋ก์ ํธ package.json์ scripts ์ถ๊ฐ
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"json-server": "json-server db.json --port 8080",
"start:dev": "concurrently \"npm run start\" \"npm run json-server\"",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
2. json-server ์๋ฒ ๋์ฐ๊ธฐ
3. ํ๋ก์ ํธ ์ต์์์ db.json ํ์ผ์ ๋ง๋ญ๋๋ค. (๊ฐ๋จํ๊ฒ ๋ง๋ค์ด ๋ดค์ต๋๋ค.)
{
"boards": [
{
"id": 1,
"nickName": "ํผ์นด์ถ",
"title": "ํ๊ธฐ ๋จ๊ฒจ์ ํผ์นด์ธ!",
"views": 21,
"likes": 24,
"diffDate": "1๋ถ์ ",
"commentCount": 3
},
{
"id": 2,
"nickName": "๋ผ์ด์ธ",
"title": "ํ๊ธฐ ๋จ๊ฒจ์ ๋ผ์ด์ธ!",
"views": 2,
"likes": 31,
"diffDate": "30๋ถ์ ",
"commentCount": 27
},
{
"id": 3,
"nickName": "๊ผฌ๋ถ์ด",
"title": "ํ๊ธฐ ๋จ๊ฒจ์! ๊ผฌ๋ถ์ด",
"views": 30,
"likes": 2,
"diffDate": "1๋ถ์ ",
"commentCount": 3
},
{
"id": 4,
"nickName": "ํ์ด๋ฆฌ",
"title": "ํ๊ธฐ ๋จ๊ฒจ์! ํ์ด๋ฆฌ",
"views": 50,
"likes": 100,
"diffDate": "1์๊ฐ์ ",
"commentCount": 3
},
{
"id": 5,
"nickName": "์ด์ํด์จ",
"title": "ํ๊ธฐ ๋จ๊ฒจ์! ์ด์ํด์จ",
"views": 0,
"likes": 0,
"diffDate": "1๋ถ์ ",
"commentCount": 0
},
{
"id": 6,
"nickName": "๋ง๋๋ฝ",
"title": "ํ๊ธฐ ๋จ๊ฒจ์! ๋ง๋๋ฝ",
"views": 2,
"likes": 20,
"diffDate": "3๋ถ์ ",
"commentCount": 3
}
]
}
4. Postman ํ์ฉํด์ API ์๋ฒ ํ ์คํธ (db.json์ ๋ฐ์ดํฐ๋ฅผ ํ์ธํ ์ ์๋ค.)
* POST, GET, PUT, DELETE ํ์ฉํด์ CRUD ํ ์คํธ๊ฐ ๊ฐ๋ฅํฉ๋๋ค!
'React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React FAQ(์์ฃผ ๋ฌป๋ ์ง๋ฌธ) ์์ฝ๋์ธ ๊ธฐ๋ฅ (35) | 2023.03.07 |
---|---|
Next.js typescript mysql styled-components ์ ํ (30) | 2023.03.03 |
[React] ๋ฆฌ๋ค์ด๋ ํธ๋ฅผ ์ด์ฉํ ๋ก๊ทธ์ธ ํ ์ด์ ํ์ด์ง ์ด๋ (26) | 2023.02.17 |
React-query ์ํ ๊ด๋ฆฌ (10) | 2022.11.26 |
React hooks useSate ์ ๋ํด์ (2) | 2022.06.09 |