⚡️ JSON-server
프론트타입 백엔드 서버
설치 : npm install -g json-server
실행 : json-server --watch db.json
GET - READ
fetch('url')
.then(response => response.json())
.then(data => {
~
})
POST - CREATE
fetch('url', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data) // 새로 저장할 데이터
})
.then(response => response.json())
.then(data => {
~
})
PUT - UPDATE
fetch('url'+id, {
method:'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data) // 새로 업데이트할 데이터
})
.then(response=>response.json())
.then(data=>{
~
})
DELETE - DELETE
fetch('url'+id,{
method:'DELETE'
})
.then(response=>response.json()) // 패턴화. 우리가 받은 데이터가 json이다라고 명시하는 것
.then(()=>{
~
})
Pending (대기) | 비동기 처리 로직이 완료되지 않았을 때 |
---|---|
Fulfiled (완료) | 비동기 처리가 결과 값을 반환해주었을 때 |
Rejected (실패) | 비동기 처리가 실패하거나 오류가 발생했을 때 |
new Promise((resolve,reject)=>{
resolve);
}).then(data=> console.log(data))
.catch((err))=> console.log(err));