3주차: node.js + express.js + REST API + ajax
NodeJS
Javascrtip와 NodeJS의 공통점/차이점
Restful, REST API
express.js에 대한 개념
AJAX
XHR(XMLHttpRequest)
Fetch API
CORS
Promise, async, await
# Request
GET /api/items
# Response
[
{ "idx": 1, "content": "todo item1", "completed": true, "createdAt": 1625484597770 },
{ "idx": 2, "content": "todo item2", "completed": false, "createdAt": 1625484600000 },
{ "idx": 3, "content": "todo item3", "completed": false, "createdAt": 1625484712340 }
]
# Request
POST /api/items
Content-Type: application/json
{
"content" : " 새로운 Todo Item"
}
# Request
# 1번 아이템의 내용을 수정
PUT /api/items/1
Content-Type: application/json
{
"content" : " 새로운 Todo Item"
}
# Request
# 1번 아이템을 토글
PUT /api/items/toggle/1
Content-Type: application/json
# Request
# 1번 아이템을 삭제
DELETE /api/items/1
Content-Type: application/json
// 아이템 조회
fetch ( '/api/items' )
. then ( response => response . json ( ) )
. then ( items => console . log ( items ) ) ;
// 아이템 추가
const addConfig = {
method : 'post' ,
headers : { 'content-type' : 'application/json' } ,
body : JSON . stringify ( { content : "새로운 Todo Item" } ) ,
}
fetch ( '/api/items' , appendConfig )
. then ( response => response . json ( ) )
. then ( items => console . log ( items ) ) ;
// 아이템 수정
const updateConfig = {
method : 'put' ,
headers : { 'content-type' : 'application/json' } ,
body : JSON . stringify ( { content : "새로운 Todo Item" } ) ,
}
fetch ( '/api/items/1' , updateConfig )
. then ( response => response . json ( ) )
. then ( items => console . log ( items ) ) ;
// 아이템 토글
fetch ( '/api/items/1/toggle' , { method : 'put' } )
. then ( response => response . json ( ) )
. then ( items => console . log ( items ) ) ;
// 아이템 삭제
fetch ( '/api/items/1' , { method : 'delete' } )
. then ( response => response . json ( ) )
. then ( items => console . log ( items ) ) ;