๊ฐ์๋ณด๊ณ ์ ๋จนํ์ ์ฐพ์๋ณด๋๊น json-server์์ ์ ๋ ฌ๋ฐฉ์์ด ์๋กญ๊ฒ ๋ฐ๋ ๊ฑฐ์๋ค .
๊ณต์๋ฌธ์์๋ ์๋์ ๊ฐ์ด ๋์์๋๋ฐ ์ฌ์ค ์ฒจ์ ๋ด๋ ์ ๋ชฐ๋๋ค. view๋ ๋๋ฒ์งธ ๊ธฐ์ค์ธ๊ฐ, ์ด๋ฌ๋ฉด์...ใ
https://www.npmjs.com/package/json-server
json-server
> [!IMPORTANT] > Viewing alpha v1 documentation โ usable but expect breaking changes. For stable version, see [here](https://github.com/typicode/json-server/tree/v0). Latest version: 1.0.0-alpha.23, last published: a month ago. Start using json-server in
www.npmjs.com

์ฌํผ ๋ค๋ฅธ ๋ธ๋ก๊ทธ๋ ์ฐพ์๋ณด๊ณ ์์์..ใ
๊ธฐ์กด ๋ฒ์ ์์๋
?_sort=createdAt&_order=desc
์ ๊ฐ์ ๋ฐฉ์์ผ๋ก, &order=asc ๋ desc ๋ฅผ ์จ์ ์ ๋ ฌํด์ฃผ์๋๋ฐ,
๋ฐ๋ ๋ฒ์ ์์๋ order ๊ฐ ์๋ sort๋ฅผ ์จ์, sort=์ ๋ ฌ๊ธฐ์ค ํน์ sort=-์ ๋ ฌ๊ธฐ์ค ์ ํตํด ๊ฐ๋จํ๊ฒ ์ค๋ฆ์ฐจ/๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ!
โจ
๊ฒฐ๊ตญ ๋ ๊ฐ๋จํด์ง !
(๊ทธ๋ฆฌ๊ณ ์ด๋ฐ๊ฑธ ํ์ด์ง๋ค์ด์ ์ด๋ผ๊ณ ํ๋๋ณด๋ค . json-server๋ ํ์ด์ง๋ค์ด์ ๊ธฐ๋ฅ์ ๋ฌด๋ฃ๋ก ํด์ค๋ค๊ณ ..)
..?_sort=id : id ๊ธฐ์ค์ผ๋ก ์ค๋ฆ ์ฐจ์
..?_sort=-id : id ๊ธฐ์ค ๋ด๋ฆผ ์ฐจ์
- ๋ฅผ ์จ์ฃผ๋ฉด ๋ด๋ฆผ์ฐจ์์ด ๋จ
(์๋ ์ฌ์ฉ ์ฝ๋ ์)
// comment-api.js
import axios from "axios";
export const commentClient = axios.create({
baseURL: import.meta.env.VITE_APP_SERVER_URL,
// process.env.REACT_APP_SERVER_URL,
headers: {
"Content-Type": "application/json",
},
});
export const getCommentsFromDB = async () => {
// ์๊ฐ ์ ์ ๋ ฌํด์ ๊ฐ์ ธ์ค๊ธฐ
const { data } = await commentClient.get("?_sort=-createdAt");
return data;
http:// .... /comments?sort=-createdAt
์ฃผ์๋ก get ์์ฒญ ๋ณด๋ด์ ์๊ฐ ์ (createdAt ๊ธฐ์ค) ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค !
+
์ฐธ๊ณ ๋ก, ๋๋ createdAt ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๋๋ฐ ์ด๊ฒ ๋ค๋ฅธ ์๊ฐ type ์ด์ฌ๋ ์ ๋ ฌ์ด ์ ๋๋ค !!! ใ ^ใ
"2023-11-03T02:07:09.423Z" ์ ๊ฐ์ ISO String (์๋ง๋) ์ ์๊ฐ ๋ฌธ์์ด๋,
"24. 2. 23. ์ค์ 12:27:12" ์ ๊ฐ์ new Date().toLocaleString ์ผ๋ก ๋ณํํ ์๊ฐ ๋ฌธ์์ด๋
๊ทธ๋ฅ ๊ฐ์ด ๋ฐ์ดํฐ์ ์์ฌ์์๋๋ฐ ์์์ ์ ์ ๋ ฌ์ด ๋์๋ค ใ ใ ๋คํ!