[20/11/11 ~ 20/11/12] - TIL | ChatterBoxClient Sprint

NOWANDHEREยท2020๋…„ 11์›” 12์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
46/53

๐ŸŒป ChatterBoxClient


๋จผ์ € ์Šคํ”„๋ฆฐํŠธ ์ง„ํ–‰์„ ์œ„ํ•ด Server, API, HTTP, Ajax, fetch ๋“ฑ์„ ๊ฒ€์ƒ‰ํ•˜์—ฌ ๋ณด๊ณ  ๊ฐœ๋…์— ๋Œ€ํ•ด ํ•™์Šตํ–ˆ๋‹ค. ๋“œ๋””์–ด Server์— ๋Œ€ํ•ด ๋ฐฐ์›Œ๋ณธ๋‹ค๋Š” ๋งˆ์Œ์— ์„ค๋ ˆ๊ธฐ๋„ ํ–ˆ๋‹ค.

Pre Course์—์„œ ์ง„ํ–‰ํ–ˆ๋˜ Twittler ์Šคํ”„๋ฆฐํŠธ์—์„œ๋Š” ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ƒˆ๋กœ๊ณ ์นจํ•˜๋ฉด ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ฌ๋ผ์ง€๋Š” ๋ฐ˜์ชฝ์งœ๋ฆฌ ์›น ์•ฑ์— ๋ถˆ๊ณผํ–ˆ๋‹ค (๋ฌผ๋ก , localStorage๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ถ€๋ถ„๊นŒ์ง€ ๊ตฌํ˜„ํ•ด๋ณด๊ธด ํ–ˆ๋‹ค). ์ด๋ฒˆ ์Šคํ”„๋ฆฐํŠธ๋Š” ์ด๋ฏธ ์ž˜ ์ž‘๋™ํ•˜๋Š” ์„œ๋ฒ„๋ฅผ ์ด์šฉํ•ด์„œ ์ฑ„ํŒ… ์•ฑ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค.

์ดํ•ดํ•˜๊ณ  ๊ตฌํ˜„ํ•˜๊ธฐ


ํŽ˜์–ด ๋ถ„๊ณผ ์–ด๋–ป๊ฒŒ ์ง„ํ–‰ํ•ด์•ผํ• ์ง€ ์ƒ์˜๋ฅผ ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ์˜ ํ•™์Šต ๋ฐฉ์‹์ด ๊ทธ๋ ‡๋“ฏ, '๋„๋Œ€์ฒด ๋ญ˜ ์–ด๋–ป๊ฒŒ ํ•˜๋ผ๋Š”๊ฑฐ์ง€?' ์‹ถ์—ˆ๋‹ค. ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›์•„์˜จ ๋ฉ”์‹œ์ง€๋“ค์„ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ์–ด์•ผ ํ–ˆ๋‹ค. ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ๋ฉ”์‹œ์ง€๋ฅผ ์„œ๋ฒ„๋กœ ๋ณด๋‚ผ์ˆ˜๋„ ์žˆ์–ด์•ผ ํ–ˆ๊ณ , chatterbox-server API ๋ฌธ์„œ๋ฅผ ์ด์šฉํ•ด ํด๋ผ์ด์–ธํŠธ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผํ–ˆ๋‹ค.

์ผ๋‹จ, ํด๋ผ์ด์–ธํŠธ ๊ตฌํ˜„์„ ์œ„ํ•ด html ํŒŒ์ผ์„ ์ˆ˜์ •ํ–ˆ๋‹ค. API ๋ฌธ์„œ๋ฅผ ๋ณด๋ฉด์„œ ๋น„๊ต์  ๊ฐ„๋‹จํ•œ GET ์š”์ฒญ๋ถ€ํ„ฐ ๋ณด๋‚ด๋ณด๊ธฐ๋กœ ํ•˜๊ณ , fetch๋ฅผ ์ด์šฉํ•ด GET ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ์‘๋‹ต์„ ๋ฌด์ž‘์ • ๊ณ„์† ์ฝ˜์†”์— ์ฐ์–ด๋ณด๋ฉฐ ์š”๊ตฌ์‚ฌํ•ญ๋“ค์„ ๊ตฌํ˜„ํ•ด๋ณด๋ ค๊ณ  ๋…ธ๋ ฅํ–ˆ๋‹ค. ์–ด๋ ค์› ๋‹ค.

GET ์š”์ฒญ์˜ ์‘๋‹ต์„ console์— ์ฐ์–ด๋ดค๋‹ค.

let message = {
  username: 'Assiduous',
  text: 'Hello',
  roomname: 'Im24'
};

// This is the url you should use to communicate with the AWS server.
const serverURL = 'http://52.78.206.149:3000/messages'
window.fetch(serverURL, {
  method: 'POST',
  body: JSON.stringify(message),
  headers: {
    "Content-Type": "application/json",
  }
}).then(response => {
  return response.json();
}).then(json => {
  console.log(json);
  // message sent!
});

์„œ๋ฒ„์— ๋ฉ”์„ธ์ง€๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ POST ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ์‘๋‹ต์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณผ์ •์ด๋‹ค. ์ด ๊ณผ์ •์„ ์ดํ•ดํ•ด๋ณด์ž. ์•„, ์„œ๋ฒ„ URL์„ fetch API๋ฅผ ํ†ตํ•ด method๋Š” POST, ํฌ์ŠคํŠธ ์š”์ฒญ์ด๊ตฌ๋‚˜, body๋Š” ๋‚ด meesage ๊ฐ์ฒด๋ฅผ JSON.stringify()์˜ ํ˜•ํƒœ๋กœ ๋ณด๋‚ด๋Š”๊ตฌ๋‚˜, HTTP ์š”์ฒญ์˜ headers๋Š” json ํ˜•ํƒœ๋ผ๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ฃผ๋Š”๊ตฌ๋‚˜, .then()์œผ๋กœ ์‘๋‹ต์„ ๋ฐ›๊ณ  .json()์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋‚ด๊ฐ€ ์•Œ์•„๋ณด๊ธฐ ์‰ฝ๊ฒŒ ๊ฐ์ฒด๋กœ ๋ฐ”๊พธ๋Š”๊ตฌ๋‚˜.


์ตœ์ข… ์ฝ”๋“œ์™€ ๊ฒฐ๊ณผ


index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>chatterbox</title>
    <link rel="stylesheet" href="styles/styles.css" />
  </head>
  <body>
    <div id="main">
      <h1>chatterbox</h1>
      <span>Room : </span>
      <select name="room" class="filter-room"></select>
      <form>
        <input type="text" class="message-input" />
        <button class="submit-button" type="submit">submit</button>
      </form>
    </div>

    <div id="chats"></div>

    <script src="scripts/app.js"></script>
  </body>
</html>

app.js

const app = {
  server: 'http://52.78.206.149:3000/messages',
  init: () => {
    // ๋ฒ„ํŠผ ํด๋ฆญ์‹œ clickSubmit ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
    const submit = document.querySelector('#main .submit-button');
    if (submit) submit.addEventListener('click', app.clickSubmit);
    // fetch(GET)๋ฅผ ํ†ตํ•ด ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์”ฉ renderํ•œ๋‹ค
    app.fetch((result) => {
      for (let i = result.length - 1; i >= 0; i--) {
        app.renderMessage(result[i]);
      }
    });
    app.makeRoomsList();
  },
  // GET
  fetch: (callback) => {
    window
      .fetch(app.server)
      .then((response) => response.json())
      .then((response) => response.results)
      .then(callback); // ์ตœ์ข… ๋ฆฌํ„ด๋œ ํ”„๋กœ๋ฏธ์Šค๊ฐ์ฒด์—์„œ ๋ฐฐ์—ด์„ ๊บผ๋‚ด callback ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰
  },
  // POST
  send: (message, callback) => {
    window
      .fetch(app.server, {
        method: 'POST',
        body: JSON.stringify(message),
        headers: {
          'Content-Type': 'application/json',
        },
      })
      .then((response) => response.json())
      .then((response) => response.results)
      .then(callback);
  },
  // ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ์˜ roomname์„ update
  makeRoomsList: () => {
    const rooms = document.querySelector('.filter-room');
    const valueObj = {};
    let value = 1;
    app.fetch((result) => {
      for (let message of result) {
        if (valueObj[message.roomname] === undefined) {
          const roomname = document.createElement('option');
          roomname.classList.add('roomName');
          roomname.innerText = message.roomname;
          roomname.value = value;
          if (rooms) rooms.appendChild(roomname);
          valueObj[message.roomname] = value++;
        }
      }
    });
  },
  clearMessages: () => {
    document.querySelector('#chats').innerHTML = '';
    // const chats = document.querySelector('#chats');
    // while (chats.hasChildNodes()) {
    //   chats.removeChild(chats.firstChild);
    // }
  },
  renderMessage: (messageObj) => {
    const message = document.createElement('div');
    message.innerText = `${messageObj.username} :\n${messageObj.text}`;
    message.classList.add('chat');
    document.querySelector('#chats').appendChild(message);
  },
  // submit ๋ฒ„ํŠผ ํด๋ฆญ์‹œ ๋‹ค ์ง€์šฐ๊ณ  ๋ฉ”์„ธ์ง€ POST ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ๋‹ค์‹œ renderํ•˜๋Š” ํ•จ์ˆ˜
  clickSubmit: (e) => {
    e.preventDefault();
    const input = document.querySelector('.message-input');
    const message = {
      username: 'Assiduous',
      text: input.value,
      roomname: 'StudyCafe',
    };
    app.clearMessages();
    app.send(message, () => {
      app.fetch((result) => {
        for (let i = result.length - 1; i >= 0; i--) {
          app.renderMessage(result[i]);
        }
      });
    });
    input.value = '';
  },
};

app.init();

ChatterBox Client Sprint ์™„์„ฑํ–ˆ๋‹ค. Reference Code๋ฅผ ๋ณด๊ณ  ๋ฆฌํŒฉํ† ๋ง๊นŒ์ง€ ๋งˆ์ณค๋‹ค. fetch API๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์‹ค์ œ ๊ตฌ๋™ํ•˜๋Š” ์„œ๋ฒ„์— GET, POST ์š”์ฒญ์„ ํ•˜๊ณ  ์‘๋‹ต์„ ๋ฐ›์•„๋ณด๋Š” ๊ฒฝํ—˜์„ ํ•  ์ˆ˜ ์žˆ์—ˆ๊ณ , ์ •๋ง ์žฌ๋ฐŒ์–ด์„œ ์ดํ‹€๋™์•ˆ ๋ฐค ๋Šฆ๊ฒŒ๊นŒ์ง€ ์‹œ๊ฐ„๊ฐ€๋Š”์ค„ ๋ชจ๋ฅด๊ณ  ์ฝ”๋”ฉ ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค. ์ด์ „์— ์ง„ํ–‰ํ–ˆ๋˜ ๋น„๋™๊ธฐ ์Šคํ”„๋ฆฐํŠธ์—์„œ fetch API๋ฅผ ์™œ ํ–ˆ์—ˆ๋Š”์ง€ ์ด์ œ์•ผ ์•Œ๊ฑฐ ๊ฐ™๋‹ค. ์ด๋ ‡๊ฒŒ ๋‚ด๊ฐ€ ๊ณต๋ถ€ํ•˜๋Š” ๊ฒƒ๋“ค์ด ์—ฐ๊ฒฐ๋˜๋Š” ์ ์ด ์ฐธ ์ข‹์€ ๊ฒƒ ๊ฐ™๋‹ค.

๋‚ด์ผ ํ•  ์ผ


  • Mini node server sprint
  • Toy Problem 10


๋ฐฐ์šฐ๋Š” ๋‹จ๊ณ„๋ผ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‹€๋ฆฐ ๋‚ด์šฉ์€ ๋Œ“๊ธ€ ๋‹ฌ์•„์ฃผ์‹œ๋ฉด ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)

profile
๐ŸŒป

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