RESTful API
What is RESTful API?
Representational
State
Transfer
WWW (World Wide Web) ์ ๊ฐ์
Hypermedia system
์ ์ํSoftware Architecture
์ค ํ๋์ ํ์REST Server
๋ client๊ฐ HTTP Protocol์ ์ฌ์ฉํ์ฌ server์ ์ ๋ณด์ ์ ๊ทผ ๋ฐ ๋ณ๊ฒฝ์ ๊ฐ๋ฅ์ผ ํจ์ ๋ณด๋
text
,xml
,json
ํ์์ผ๋ก ์ ๊ณต๋จ์์ฆ์
json
์ด ๋์ธ!
HTTP method
REST ๊ธฐ๋ฐ architecture์์ ์์ฃผ ์ฌ์ฉ๋๋ 4๊ฐ์ง methods
GET
: ์กฐํ
PUT
: ์์ฑ ๋ฐ
์ ๋ฐ์ดํธ
DELETE
: ์ ๊ฑฐ
POST
: ์์ฑ
Database ์์ฑ
JSON๊ธฐ๋ฐ์ user database ๋ง๋ค๊ธฐ
Test04_RESTful > data > user.json
1. API: GET/list
๋ชจ๋ user list ์ถ๋ ฅํ๋
GET API
๋ง๋ค๊ธฐ
router > main.js
__dirname
ํ์ฌ module์ ์์น๋ฅผ ๋ํ๋
router module์ router folder์์ ์์ด์ data folder์ ์ ๊ทผํ๋ ค๋ฉด ์๋๊ฒฝ๋ก ์ด์ฉํด์ผํจ!
server ์คํ ํ http://localhost/list ์ ์
2. API: GET/getUser/:username
ํน์ username์ detailํ ์ ๋ณด ๊ฐ์ ธ์ค๋
GET API
๋ง๋ค๊ธฐ!
router > main.js ์ ์ถ๊ฐ
file ์ฝ์ ํ, user id๋ฅผ ์ฐพ์์ ์ถ๋ ฅํด์ค
user๋ฅผ ์ฐพ์ผ๋ฉด user data ์ถ๋ ฅ
์์ผ๋ฉด {} ์ถ๋ ฅ
fs.readFile()
๋ก file์ ์ฝ์์ ์์ text ํํ๋ก ์ฝ์ด์ง๊ธฐ ๋๋ฌธ์JSON.parse()
๋ฅผ ํด์ผํ๋ค!
server ๋ค์ ์คํ ํ http://localhost/getUser/first_user ์ ์
3. API: POST addUser/:username
body: { โpasswordโ: โโ, โnameโ: โโ }
router > main.js ์ ์ถ๊ฐ
JSON
ํํ๊ฐ Invalid ํ๋ฉด ์ค๋ฅ ๋ฐํvalid ํ๋ฉด ํ์ผ์ ์ด์ด์
username
์ ์ค๋ณต์ฑ์ ํ์ธ ํJSON
data์ ์ถ๊ฐํ์ฌ ๋ค์ ์ ์ฅstringify(users, null, 2)
์ JSON ์ pretty-print ๋ฅผ ์ํจ
4. API: PUT updateUser/:username
body: { โpasswordโ: โโ, โnameโ: โโ }
์ฌ์ฉ์ ์ ๋ณด๋ฅผ updateํ๋ API
PUT
method ์ฌ์ฉidempotent ํ
PUT API
์์ฒญ์ ๋ช ๋ฒ ์ํํ๋๋ผ๋, ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฅํด์ผํจ!
router > main.js ์ ์ถ๊ฐ
5. API: DELETE deleteUser/:username
์ฌ์ฉ์ data๋ฅผ ์ญ์ ํ๋ API
DELETE
method ์ฌ์ฉ
router > main.js์ ์ถ๊ฐ
Last updated
Was this helpful?