npm install -g json-server
db.json
생성(가짜 DB 역할){
"posts": [{ "id": 1, "title": "json-server", "author": "typicode" }],
"comments": [{ "id": 1, "body": "some comment", "postId": 1 }],
"profile": { "name": "typicode" }
}
json-server --watch db.json
http://localhost:3000/posts
http://localhost:3000/comments
http://localhost:3000/profile
3001로 port 번호 설정
json-server --watch data.json --port 3001
function getData() {
//조회 get 요청
fetch("http://localhost:3000/posts")
.then((response) => response.json())
.then((json) => console.log(json));
}
//삽입 post 요청
function postData() {
const newData = {
name: "HHHHH",
company: "DCU",
email: "HHHHH@cu.ac.kr",
phone: "010-0000-0000",
address: "Daecu",
};
fetch("http://localhost:3000/myData", {
method: "POST",
body: JSON.stringify(newData),
headers: {
"content-type": "application/json; charset=UTF-8",
},
})
.then((response) => response.json())
.then((json) => console.log(json));
}
//수정 put 요청
function putData() {
const newData = {
name: "HHHHH",
company: "DCU",
email: "HHHHH@cu.ac.kr",
phone: "010-0000-0000",
address: "Daecu",
};
fetch("http://localhost:3000/myData/(!아이디값!)", {
method: "PUT",
body: JSON.stringify(newData),
headers: {
"content-type": "application/json; charset=UTF-8",
},
})
.then((response) => response.json())
.then((json) => console.log(json));
}
//삭제 DELETE 요청
function deleteData() {
fetch("http://localhost:3000/myData/63591480d5ac0ddd89509078", {
method: "DELETE",
})
.then((response) => response.json())
.then((json) => console.log(json));
}