REST API ๐Ÿ˜ฎ

c_yjยท2022๋…„ 7์›” 20์ผ
0

DeepDive

๋ชฉ๋ก ๋ณด๊ธฐ
38/42
post-thumbnail

REST๋Š” HTTP๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์˜ ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ์‹์„ ๊ทœ์ •ํ•œ ์•„ํ‚คํ…์ฒ˜๊ณ , REST API๋Š” REST๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์„œ๋น„์Šค API๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

REST API์˜ ๊ตฌ์„ฑ ๐Ÿ˜—

REST API๋Š” ์ž์›, ํ–‰์œ„, ํ‘œํ˜„์˜ 3๊ฐ€์ง€ ์š”์†Œ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. REST๋Š” ์ž์ฒด ํ‘œํ˜„ ๊ตฌ์กฐ๋กœ ๊ตฌ์„ฑ๋˜์–ด REST API๋งŒ์œผ๋กœ HTTP ์š”์ฒญ์˜ ๋‚ด์šฉ์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ตฌ์„ฑ์š”์†Œ๋‚ด์šฉํ‘œํ˜„ ๋ฐฉ๋ฒ•
์ž์›์ž์›URI(์—”๋“œํฌ์ธํŠธ)
ํ–‰์œ„์ž์›์— ๋Œ€ํ•œ ํ–‰์œ„HTTP ์š”์ฒญ ๋ฉ”์„œ๋“œ
ํ‘œํ˜„์ง€์›์— ๋Œ€ํ•œ ํ–‰์œ„์— ๊ตฌ์ฒด์  ๋‚ด์šฉํŽ˜์ด๋กœ๋“œ

REST API์˜ ์„ค๊ณ„ ์›์น™ ๐Ÿ˜

REST์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ธฐ๋ณธ์ ์ธ ์›์น™์€ ๋‘ ๊ฐ€์ง€๋‹ค. URI๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐ ์ง‘์ค‘ํ•˜๊ณ  ํ–‰์œ„์— ๋Œ€ํ•œ ์ •์˜๋Š” HTTP ์š”์ฒญ ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ํ•˜๋Š” ๊ฒƒ์ด RESTful API๋ฅผ ์„ค๊ณ„ํ•˜๋Š” ์ค‘์‹ฌ ๊ทœ์น™์ด๋‹ค.

1. URI๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ ํ‘œํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

URI๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐ ์ค‘์ ์„ ๋‘์–ด์•ผ ํ•œ๋‹ค. ๋ฆฌ์†Œ์Šค๋ฅผ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ์ด๋ฆ„์€ ๋™์‚ฌ๋ณด๋‹ค๋Š” ๋ช…์‚ฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋ฆ„์— get ๊ฐ™์€ ํ–‰์œ„์— ๋Œ€ํ•œ ํ‘œํ˜„์ด ๋“ค์–ด๊ฐ€์„œ๋Š” ์•ˆ ๋œ๋‹ค.

#bad
GET /getTodos/1
GET /todos/show/1

#good
GET/todos/1

2. ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ํ–‰์œ„๋Š” HTTP ์š”์ฒญ ๋ฉ”์„œ๋“œ๋กœ ํ‘œํ˜„ํ•œ๋‹ค.

HTTP ์š”์ฒญ ๋ฉ”์„œ๋“œ๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์—๊ฒŒ ์š”์ฒญ์˜ ์ข…๋ฅ˜์™€ ๋ชฉ์ ์„ ์•Œ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ฃผ๋กœ 5๊ฐ€์ง€ ์š”์ฒญ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ CRUD๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

HTTP ์š”์ฒญ ๋ฉ”์„œ๋“œ์ข…๋ฅ˜๋ชฉ์ ํŽ˜์ด๋กœ๋“œ
GETindex/retrieve๋ชจ๋“ /ํŠน์ • ๋ฆฌ์†Œ์Šค ์ทจ๋“X
POSTcreate๋ฆฌ์†Œ์Šค ์ƒ์„ฑO
PUTreplace๋ฆฌ์†Œ์Šค์˜ ์ „์ฒด ๊ต์ฒดO
PATCHmodify๋ฆฌ์†Œ์Šค์˜ ์ผ๋ถ€ ์ˆ˜์ •O
DELETEdelete๋ชจ๋“ /ํŠน์ • ๋ฆฌ์†Œ์Šค ์‚ญ์ œX

๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ํ–‰์œ„๋Š” HTTP ์š”์ฒญ ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ํ‘œํ˜„ํ•˜๋ฉฐ URI์— ํ‘œํ˜„ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฆฌ์†Œ์Šค๋ฅผ ์ทจ๋“ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” GET, ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ญ์ œํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” DELETE๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ํ–‰์œ„๋ฅผ ๋ช…ํ™•ํžˆ ํ‘œํ˜„ํ•œ๋‹ค.

JSON Server๋ฅผ ์ด์šฉํ•œ REST API ์‹ค์Šต ๐Ÿ˜™

HTTP ์š”์ฒญ์„ ์ „์†กํ•˜๊ณ  ์‘๋‹ต์„ ๋ฐ›์œผ๋ ค๋ฉด ์„œ๋ฒ„๊ฐ€ ํ•„์š”ํ•˜๋‹ค. JSON Server๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ€์ƒ REST API ์„œ๋ฒ„๋ฅผ ๊ตฌ์ถ•ํ•˜์—ฌ HTTP ์š”์ฒญ์„ ์ „์†กํ•˜๊ณ  ์‘๋‹ต์„ ๋ฐ›๋Š” ์‹ค์Šต์„ ํ•ด๋ณด์ž.

JSON Server ์„ค์น˜

JSON Server๋Š” json ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ƒ REST API ์„œ๋ฒ„๋ฅผ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋Š” ํˆด์ด๋‹ค. ์‚ฌ์šฉ๋ฒ•์€ ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋‹ค. ๋จผ์ € npm์„ ์‚ฌ์šฉํ•˜์—ฌ JSON Server๋ฅผ ์„ค์น˜ํ•˜์ž.

ํ„ฐ๋ฏธ๋„์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜์—ฌ JSON Server๋ฅผ ์„ค์น˜ํ•œ๋‹ค.
$ mkdir json-server-exam && cd json-server-exam
$ npm init -y
$ npm install json-server --save-dev

db.json ํŒŒ์ผ ์ƒ์„ฑ

ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ ํด๋”(/json-server-exam)์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด db.json ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค. db.json ํŒŒ์ผ์€ ๋ฆฌ์†Œ์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ญํ• ์„ ํ•œ๋‹ค.

{
    "todos": [
        {
            "id":1,
            "content": "HTML",
            "completed": true
        },
        {
            "id":2,
            "content": "CSS",
            "completed": false
        },
        {
            "id":3,
            "content": "JavaScript",
            "completed": true
        },
    ]
}

JSON Server ์‹คํ–‰

ํ„ฐ๋ฏธ๋„์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜์—ฌ JSON Server๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. JSON Server๊ฐ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ญํ• ์„ ํ•˜๋Š” db.json ํŒŒ์ผ์˜ ๋ณ€๊ฒฝ์„ ๊ฐ์ง€ํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด watch ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

##๊ธฐ๋ณธ ํฌํŠธ(3000) ์‚ฌ์šฉ / watch ์˜ต์…˜ ์ ์šฉ
$ json-server --watch db.json

GET ์š”์ฒญ ๐Ÿ˜

todos ๋ฆฌ์†Œ์Šค์—์„œ ๋ชจ๋“  todo๋ฅผ ์ทจ๋“(index)ํ•œ๋‹ค.
JSON Server์˜ ๋ฃจํŠธ ํด๋”(/json-server-exam)์— public ํด๋”๋ฅผ ์ƒ์„ฑํ•˜๊ณ  JSON Server๋ฅผ ์ค‘๋‹จํ•œ ํ›„ ์žฌ์‹คํ–‰ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  public ํด๋”์— ๋‹ค์Œ get_index.html์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ธŒ๋ผ์šฐ์ €์—์„œ http://localhost:3000/get_index.html๋กœ ์ ‘์†ํ•œ๋‹ค

<!DOCTYPE html>
<html>
<body>
    <pre></pre>
    <script>
        // XMLHttpRequest ๊ฐ์ฒด ์ƒ์„ฑ
        const xhr = new XMLHttpRequest();
        // HTTP ์š”์ฒญ ์ดˆ๊ธฐํ™”
        // todos ๋ฆฌ์†Œ์Šค์—์„œ ๋ชจ๋“  todo๋ฅผ ์ทจ๋“(index)
        xhr.open('GET', '/todos');

        //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>

todos ๋ฆฌ์†Œ์Šค์—์„œ id๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ todo๋ฅผ ์ทจ๋“(retrieve)ํ•œ๋‹ค. public ํด๋”์— ๋‹ค์Œ get_retrieve.html์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ธŒ๋ผ์šฐ์ €์—์„œ http://localhost:3000/get_retrieve.html๋กœ ์ ‘์†ํ•œ๋‹ค

<!DOCTYPE html>
<html>
  <body>
    <pre></pre>
    <script>
      const xhr = new XMLHttpRequest();

      xhr.open("GET", "/todos/1");

      xhr.send();

      xhr.onload = () => {
        if (xhr.status === 200) {
          document.querySelector('pre').textContent = xhr.response;
        } else {
          console.error("Error", xhr.status, xhr.statusText);
        }
      };
    </script>
  </body>
</html>

POST ์š”์ฒญ ๐Ÿ˜ฎ

todos ๋ฆฌ์†Œ์Šค์— ์ƒˆ๋กœ์šด todo๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. POST ์š”์ฒญ ์‹œ์—๋Š” setRequestHeader ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์š”์ฒญ ๋ชธ์ฒด์— ๋‹ด์•„ ์„œ๋ฒ„๋กœ ์ „์†กํ•  ํŽ˜์ด๋กœ๋“œ์˜ MIME ํƒ€์ž…์„ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.
public ํด๋”์— ๋‹ค์Œ post.html์„ ์ถ”๊ฐ€ํ•˜๊ณ  http://localhost:3000/post.html๋กœ ์ ‘์†ํ•œ๋‹ค.

<!DOCTYPE html>
<html>
<body>
  <pre></pre>
  <script>
    // XMLHttpRequest ๊ฐ์ฒด ์ƒ์„ฑ
    const xhr = new XMLHttpRequest();

    // HTTP ์š”์ฒญ ์ดˆ๊ธฐํ™”
    // todos ๋ฆฌ์†Œ์Šค์— ์ƒˆ๋กœ์šด todo๋ฅผ ์ƒ์„ฑ
    xhr.open('POST', '/todos');

    // ์š”์ฒญ ๋ชธ์ฒด์— ๋‹ด์•„ ์„œ๋ฒ„๋กœ ์ „์†กํ•  ํŽ˜์ด๋กœ๋“œ์˜ MIME ํƒ€์ž…์„ ์ง€์ •
    xhr.setRequestHeader('content-type', 'application/json');

    // HTTP ์š”์ฒญ ์ „์†ก
    // ์ƒˆ๋กœ์šด todo๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ํŽ˜์ด๋กœ๋“œ๋ฅผ ์„œ๋ฒ„์— ์ „์†กํ•ด์•ผ ํ•œ๋‹ค.
    xhr.send(JSON.stringify({ id: 4, content: 'Angular', 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์€ ํŠน์ • ๋ฆฌ์†Œ์Šค ์ „์ฒด๋ฅผ ๊ต์ฒดํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ์—์„œ๋Š” todos ๋ฆฌ์†Œ์Šค์—์„œ id๋กœ todo๋ฅผ ํŠน์ •ํ•˜์—ฌ id๋ฅผ ์ œ์™ธํ•œ ๋ฆฌ์†Œ์Šค ์ „์ฒด๋ฅผ ๊ต์ฒดํ•œ๋‹ค. PUT ์š”์ฒญ ์‹œ์—๋Š” setRequestHeader ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์š”์ฒญ ๋ชธ์ฒด์— ๋‹ด์•„ ์„œ๋ฒ„๋กœ ์ „์†กํ•  ํŽ˜์ด๋กœ๋“œ์˜ MIME ํƒ€์ž…์„ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.
public ํด๋”์— ๋‹ค์Œ put.html์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ธŒ๋ผ์šฐ์ €์—์„œ http://localhost:3000/put.html๋กœ ์ ‘์†ํ•œ๋‹ค

<!DOCTYPE html>
<html>
<body>
  <pre></pre>
  <script>
    // XMLHttpRequest ๊ฐ์ฒด ์ƒ์„ฑ
    const xhr = new XMLHttpRequest();

    // HTTP ์š”์ฒญ ์ดˆ๊ธฐํ™”
    // todos ๋ฆฌ์†Œ์Šค์—์„œ id๋กœ todo๋ฅผ ํŠน์ •ํ•˜์—ฌ id๋ฅผ ์ œ์™ธํ•œ ๋ฆฌ์†Œ์Šค ์ „์ฒด๋ฅผ ๊ต์ฒด
    xhr.open('PUT', '/todos/4');

    // ์š”์ฒญ ๋ชธ์ฒด์— ๋‹ด์•„ ์„œ๋ฒ„๋กœ ์ „์†กํ•  ํŽ˜์ด๋กœ๋“œ์˜ MIME ํƒ€์ž…์„ ์ง€์ •
    xhr.setRequestHeader('content-type', 'application/json');

    // HTTP ์š”์ฒญ ์ „์†ก
    // ๋ฆฌ์†Œ์Šค ์ „์ฒด๋ฅผ ๊ต์ฒดํ•˜๊ธฐ ์œ„ํ•ด ํŽ˜์ด๋กœ๋“œ๋ฅผ ์„œ๋ฒ„์— ์ „์†กํ•ด์•ผ ํ•œ๋‹ค.
    xhr.send(JSON.stringify({ id: 4, content: 'React', completed: true }));

    // 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>

PATCH ์š”์ฒญ ๐Ÿ˜ƒ

PATCH๋Š” ํŠน์ • ๋ฆฌ์†Œ์Šค์˜ ์ผ๋ถ€๋ฅผ ์ˆ˜์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ์—์„œ๋Š” todos ๋ฆฌ์†Œ์Šค์˜ id๋กœ todo๋ฅผ ํŠน์ •ํ•˜์—ฌ completed๋งŒ ์ˆ˜์ •ํ•œ๋‹ค. PATCH ์š”์ฒญ ์‹œ์—๋Š” setRequestHeader ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์š”์ฒญ ๋ชธ์ฒด์— ๋‹ด์•„ ์„œ๋ฒ„๋กœ ์ „์†กํ•  ํŽ˜์ด๋กœ๋“œ์˜ MIME ํƒ€์ž…์„ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.
public ํด๋”์— ๋‹ค์Œ patch.html์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ธŒ๋ผ์šฐ์ €์—์„œ http://localhost:3000/patch.html๋กœ ์ ‘์†ํ•œ๋‹ค.

<!DOCTYPE html>
<html>
<body>
  <pre></pre>
  <script>
    // XMLHttpRequest ๊ฐ์ฒด ์ƒ์„ฑ
    const xhr = new XMLHttpRequest();

    // HTTP ์š”์ฒญ ์ดˆ๊ธฐํ™”
    // todos ๋ฆฌ์†Œ์Šค์˜ id๋กœ todo๋ฅผ ํŠน์ •ํ•˜์—ฌ completed๋งŒ ์ˆ˜์ •
    xhr.open('PATCH', '/todos/4');

    // ์š”์ฒญ ๋ชธ์ฒด์— ๋‹ด์•„ ์„œ๋ฒ„๋กœ ์ „์†กํ•  ํŽ˜์ด๋กœ๋“œ์˜ MIME ํƒ€์ž…์„ ์ง€์ •
    xhr.setRequestHeader('content-type', 'application/json');

    // HTTP ์š”์ฒญ ์ „์†ก
    // ๋ฆฌ์†Œ์Šค๋ฅผ ์ˆ˜์ •ํ•˜๊ธฐ ์œ„ํ•ด ํŽ˜์ด๋กœ๋“œ๋ฅผ ์„œ๋ฒ„์— ์ „์†กํ•ด์•ผ ํ•œ๋‹ค.
    xhr.send(JSON.stringify({ completed: false }));

    // 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>

PATCH ์š”์ฒญ ๐Ÿ˜‰

todos ๋ฆฌ์†Œ์Šค์—์„œ id๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ todo๋ฅผ ์‚ญ์ œํ•œ๋‹ค. public ํด๋”์— ๋‹ค์Œ delete.html์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ธŒ๋ผ์šฐ์ €์—์„œ http://localhost:3000/delete.html๋กœ ์ ‘์†ํ•œ๋‹ค

<!DOCTYPE html>
<html>
<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์ด๋ฉด ์ •์ƒ์ ์œผ๋กœ ์‘๋‹ต๋œ ์ƒํƒœ๋‹ค.
      if (xhr.status === 200) {
        document.querySelector('pre').textContent = xhr.response;
      } else {
        console.error('Error', xhr.status, xhr.statusText);
      }
    };
  </script>
</body>
</html>

profile
FrontEnd Developer

0๊ฐœ์˜ ๋Œ“๊ธ€