프론트에서 필요한 서버 설치
https://www.npmjs.com/package/json-server
json-server
[](https://github.com/typicode/json-server/actions/workflows/node.js.yml). Latest version: 1.0.0-beta.3, last published: 2 months ago. Start using json-server in
www.npmjs.com
버전에따라 설치
npm install json-server@0.17.4
터미널에 입력
npx json-server --watch db.json --port 500
locallhost:500
들어가서 확인
http://localhost:500/products/1

비주얼스튜디오에서 새터미널 열어서
cd project 들어가서 npm start
locallhost3000(react) 터미널 1개
locallhost500(json) 터미널 1개 열려있으므로 2개의 터미널이 열려있어야한다


const ProductAll = () => {
const getProducts =async()=>{
let url = `http://localhost:500/products/`
let response = await fetch(url);
let data = await response.json();
console.log(data)
}
//설명
const ProductAll = () => {
// getProducts라는 비동기 함수 정의
const getProducts = async () => {
// API 요청을 보낼 URL
let url = `http://localhost:500/products/`;
// fetch 메서드로 데이터를 가져오고 응답 객체를 response에 저장
let response = await fetch(url);
// 응답 데이터를 JSON 형식으로 변환하여 data에 저장
let data = await response.json();
// 변환된 데이터를 콘솔에 출력
console.log(data);
};
// 컴포넌트의 나머지 로직이 들어갈 수 있음
};

'REACT' 카테고리의 다른 글
| REACT_Props 지피티의설명을내가수정 (1) | 2024.11.20 |
|---|---|
| REACT_화살표 함수 지피티의 정리를내가또정리하기수정중 (0) | 2024.11.20 |
| REACT_Router (0) | 2024.11.18 |
| REACT_HOC (Higher-Order Component) 하이오더컴포넌트 구하기 (0) | 2024.09.25 |
| REACT_Ref (0) | 2024.09.25 |