API
์์คํ ์ด ๋ง๋ค์ด ๋์ ์๋น์ค ์ฐฝ๊ตฌ
REST API
- REpresentational State Transfer
- ํด๋ผ์ด์ธํธ์ ์๋ฒ๊ฐ ์ฃผ๊ณ ๋ฐ๋ ๋ฉ์์ง(๋ฐ์ดํฐ) ํ์
- HTTP ๋ฉ์๋(POST, GET, PUT, DELETE)๋ฅผ ํตํด ํด๋น ์์์ ๋ํ CRUD๋ฅผ ์ ์ฉ
JSON Server๋ฅผ ์ด์ฉํ REST API ์ค์ต
1. JSON Server ์ค์น
mkdir json-server-exam && cd json-server-exam
npm init -y
npm install json-server --save-dev
2. db.json ํ์ผ ์์ฑ
/json-server-exam/db.json
{
"todos":[
{
"id":1,
"content":"HTML",
"completed":true
},
{
"id":2,
"content":"CSS",
"completed":false
},
{
"id":1,
"content":"JavaScript",
"completed":true
}
]
}
3. JSON Server ์คํ
## ๊ธฐ๋ณธ ํฌํธ ์ฌ์ฉ(3000)/watch ์ต์
์ ์ฉ
json-server --watch db.json
## ํฌํธ ๋ณ๊ฒฝ / watch ์ต์
์ ์ฉ
json-server --watch db.json --port 5000
4. package.json ์์
{
"name": "json-server-exam",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \\"Error: no test specified\\" && exit 1",
**"start":"json-server --watch db.json"**
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"json-server": "^0.17.1"
}
}
5. JSON Server ์คํ
npm start
GET ์์ฒญ
json-server-exam/public/get_index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<pre></pre>
<script>
//XMLHttpRequest ๊ฐ์ฒด ์์ฑ
const xhr = new XMLHttpRequest()
//HTTP ์์ฒญ ์ด๊ธฐํ
//todos ๋ฆฌ์์ค์ ๋ชจ๋ todo๋ฅผ ์ทจ๋(index)
//์ ์ฒด
xhr.open('GET','/todos')
//์์ด๋ ๊ฐ
xhr.open('GET','/todos/1')
//HTTP ์์ฒญ ์ ์ก
xhr.send()
//load ์ด๋ฒคํธ๋ ์์ฒญ์ด ์ฑ๊ณต์ ์ผ๋ก ์๋ฃ๋ ๊ฒฝ์ฐ ๋ฐ์
xhr.onload = () => {
//status ํ๋กํผํฐ ๊ฐ์ด 200์ด๋ฉด ์ ์์ ์ผ๋ก ์๋ต๋ ์ํ
if(xhr.status ===200){
document.querySelector('pre').textContent = xhr.response
}else{
console.error('Error',xhr.status,xhr.statusText)
}
}
</script>
</body>
</html>
POST ์์ฒญ
- POST ์์ฒญ ์์๋ setRequestHeader ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ์์ฒญ ๋ชธ์ฒด์ ๋ด์ ์๋ฒ๋ก ์ ์กํ ํ์ด๋ก๋์ MINE ํ์ ์ง์ ํด์ผ ํจ
json-server-exam/public/post.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<pre></pre>
<script>
//XMLHttpRequest ๊ฐ์ฒด ์์ฑ
const xhr = new XMLHttpRequest()
//HTTP ์์ฒญ ์ด๊ธฐํ
//todos ๋ฆฌ์์ค์ ์๋ก์ด todo๋ฅผ ์์ฑ
xhr.open('POST','/todos')
//์์ฒญ ๋ชธ์ฒด์ ๋ด์ ์๋ฒ๋ก ์ ์กํ ํ์ด๋ก๋์ MINE ํ์
์ ์ง์
xhr.setRequestHeader('content-type','application/json')
//HTTP ์์ฒญ ์ ์ก
//์๋ก์ด todo๋ฅผ ์์ฑํ๊ธฐ ์ํด ํ์ด๋ก๋๋ฅผ ์๋ฒ์ ์ ์ก
xhr.send(JSON.stringify({id:4, content:'Next.js',completed:false}))
//load ์ด๋ฒคํธ๋ ์์ฒญ์ด ์ฑ๊ณต์ ์ผ๋ก ์๋ฃ๋ ๊ฒฝ์ฐ ๋ฐ์
xhr.onload = () => {
//status ํ๋กํผํฐ ๊ฐ์ด 200(ok)๋๋ 201(created)์ด๋ฉด ์ ์์ ์ผ๋ก ์๋ต๋ ์ํ
if(xhr.status ===200 || xhr.status==201){
document.querySelector('pre').textContent = xhr.response
}else{
console.error('Error',xhr.status,xhr.statusText)
}
}
</script>
</body>
</html>
PUT ์์ฒญ
- ํน์ ๋ฆฌ์์ค ์ ์ฒด๋ฅผ ๊ต์ฒดํ ๋ ์ฌ์ฉ
- PUT ์์ฒญ์์ setRequestHeader ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์์ฒญ ๋ชธ์ฒด์ ๋ด์ ์๋ฒ๋ก ์ ์กํ ํ์ด๋ก๋์ MINE ํ์ ์ ์ง์
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <pre></pre> <script> //XMLHttpRequest ๊ฐ์ฒด ์์ฑ const xhr = new XMLHttpRequest() //HTTP ์์ฒญ ์ด๊ธฐํ //todos ๋ฆฌ์์ค์ id๋ก todo๋ฅผ ํน์ ํ์ฌ id๋ฅผ ์ ์ธํ ๋ฆฌ์์ค ์ ์ฒด๋ฅผ ๊ต์ฒด xhr.open('PUT','/todos/4') //์์ฒญ ๋ชธ์ฒด์ ๋ด์ ์๋ฒ๋ก ์ ์กํ ํ์ด๋ก๋์ MINE ํ์ ์ ์ง์ xhr.setRequestHeader('content-type','application/json') //HTTP ์์ฒญ ์ ์ก //์๋ก์ด todo๋ฅผ ์์ฑํ๊ธฐ ์ํด ํ์ด๋ก๋๋ฅผ ์๋ฒ์ ์ ์ก xhr.send(JSON.stringify({id:4, content:'Next.js',completed:true})) //load ์ด๋ฒคํธ๋ ์์ฒญ์ด ์ฑ๊ณต์ ์ผ๋ก ์๋ฃ๋ ๊ฒฝ์ฐ ๋ฐ์ xhr.onload = () => { //status ํ๋กํผํฐ ๊ฐ์ด 200(ok)๋๋ 201(created)์ด๋ฉด ์ ์์ ์ผ๋ก ์๋ต๋ ์ํ if(xhr.status ===200){ document.querySelector('pre').textContent = xhr.response }else{ console.error('Error',xhr.status,xhr.statusText) } } </script> </body> </html>
DELETE ์์ฒญ
- todos ๋ฆฌ์์ค์์ id๋ฅผ ์ฌ์ฉํ์ฌ todo๋ฅผ ์ญ์
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<pre></pre>
<script>
//XMLHttpRequest ๊ฐ์ฒด ์์ฑ
const xhr = new XMLHttpRequest()
//HTTP ์์ฒญ ์ด๊ธฐํ
//todos ๋ฆฌ์์ค์ id๋ฅผ ์ฌ์ฉํด todo ์ญ์
xhr.open('DELETE','/todos/4')
//HTTP ์์ฒญ ์ ์ก
xhr.send()
//load ์ด๋ฒคํธ๋ ์์ฒญ์ด ์ฑ๊ณต์ ์ผ๋ก ์๋ฃ๋ ๊ฒฝ์ฐ ๋ฐ์
xhr.onload = () => {
//status ํ๋กํผํฐ ๊ฐ์ด 200(ok)๋๋ 201(created)์ด๋ฉด ์ ์์ ์ผ๋ก ์๋ต๋ ์ํ
if(xhr.status ===200){
document.querySelector('pre').textContent = xhr.response
}else{
console.error('Error',xhr.status,xhr.statusText)
}
}
</script>
</body>
</html>
Reference
์ด์ ๋ชจ ์ , ใ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ Deep Diveใ, ์ํค๋ถ์ค(2020)
'Language > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] Event-onFocus, onBlur (0) | 2023.01.20 |
---|---|
[JavaScript] ๋น๋๊ธฐ (0) | 2023.01.17 |
[JavaScript] AJAX & JSON (0) | 2023.01.15 |
[FE/JavaScript] ๊ตฌ์กฐ ๋ถํด ํ ๋น(๋์คํธ๋ญ์ฒ๋ง ํ ๋น) (0) | 2022.07.06 |
[FE/JavaScript] strict mode (0) | 2022.06.27 |
๋๊ธ