ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JS] chapter22. ๋น„๋™๊ธฐ4 - Axios
    codeStates front-end/Java Script 2023. 1. 19. 17:51
    ๋ฐ˜์‘ํ˜•

     

     

     

    ๐Ÿ“Œ ๋น„๋™๊ธฐ

     

     

    ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ž€, ํŠน์ • ์ฝ”๋“œ์˜ ์‹คํ–‰์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ  ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ

    js๋Š” ์‹ฑ๊ธ€ ์Šค๋ ˆ๋“œ ๊ธฐ๋ฐ˜ ๋™์ž‘ ์–ธ์–ด๋ผ์„œ ๋™๊ธฐ์  ์ž‘๋™์ด์ง€๋งŒ ํ™˜๊ฒฝ(๋Ÿฐํƒ€์ž„)์—์„œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ๋„์™€์ฃผ๊ธฐ ๋•Œ๋ฌธ์—

    ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๋˜ํ•œ ๊ฐ€๋Šฅํ•˜๋‹ค.

     

     

    ๐Ÿ’ป Axios

     

    fetch API์™€ ๋น„์Šทํ•œ ์—ญํ• ์„ ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ 

    Axios๋Š” ๋ธŒ๋ผ์šฐ์ €, Node.js๋ฅผ ์œ„ํ•œ Promise API๋ฅผ ํ™œ์šฉํ•˜๋Š” HTTP ๋น„๋™๊ธฐ ํ†ต์‹  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

     

     

    Fetch API vs Axios

     

    Axios Fetch API
    ์จ๋“œํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์„ค์น˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋นŒํŠธ์ธ API๋ผ ๋ณ„๋„์˜ ์„ค์น˜ ํ•„์š”์—†์Šต๋‹ˆ๋‹ค.
    ์ž๋™์œผ๋กœ JSON๋ฐ์ดํ„ฐ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. .json() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

     

     

    Axios ์‚ฌ์šฉ๋ฒ•

     

    npm install axios // axios ์„ค์น˜

     

    GET ์š”์ฒญ

     

    ์ •๋ณด๋ฅผ ์š”์ฒญํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ

    ์ฒซ๋ฒˆ์งธ ์ธ์ž : URL , ๋‘๋ฒˆ์งธ ์ธ์ž : ์š”์ฒญ์‹œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜๋“ค

    axios.get("url"[,config])

     

     

     

    ๐Ÿ“ fetch API์™€ axios GET ์š”์ฒญ์˜ ์‚ฌ์šฉ๋ฒ•๊ณผ ์ฐจ์ด์ 

     

    fetch API

     

    fetch('https://koreanjson.com/users/1', { method: 'GET' })
      .then((response) => response.json())
      .then((json) => console.log(json))
      .catch((error) => console.log(error));
    
    // Async / Await ver
    // async function request() {
    //   const response = await fetch('https://koreanjson.com/users/1', {
    //     method: 'GET',
    //   });
    //   const data = await response.json();
    //   console.log(data);
    // }
    
    // request();
    
    const appDiv = document.getElementById('app');
    appDiv.innerHTML = `
    <h1>Fetch API ๐Ÿ˜Š</h1>
    <h3>console ์ฐฝ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”! ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡</h3>
    `;

     

    axios GET 

     

    // axios๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์„ค์น˜ํ•œ axios๋ฅผ ๋ถˆ๋Ÿฌ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    import axios from 'axios';
    
    // Promise ver
    axios
      .get('https://koreanjson.com/users/1')
      .then((response) => {
        const { data } = response;
        console.log(data);
      })
      .catch((error) => console.log(error));
    
    // Async / Await ver
    // async function request() {
    //   const response = await axios.get('https://koreanjson.com/users/1');
    //   const { data } = response;
    //   console.log(data);
    // }
    
    // request();
    
    const appDiv = document.getElementById('app');
    appDiv.innerHTML = `
    <h1>Axios โ˜บ๏ธ</h1>
    <h3>console ์ฐฝ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”! ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡</h3>
    `;

     

    ์ฝœ์†” ๊ฒฐ๊ณผ (๋‘ ๊ฐ’ ๋™์ผ)

     

     

     

    POST ์š”์ฒญ

     

    ์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ

    ์ฒซ๋ฒˆ์งธ ์ธ์ž : URL , ๋‘๋ฒˆ์งธ ์ธ์ž : ์š”์ฒญ ์‹œ ๋ณด๋‚ผ ๋ฐ์ดํ„ฐ๋ฅผ ์„ค์ •

     

    axios.post("url"[, data[, config]])

     

     

    ๐Ÿ“ fetch API์™€ axios POST ์š”์ฒญ์˜ ์‚ฌ์šฉ๋ฒ•๊ณผ ์ฐจ์ด์ 

     

     

    Fetch API์˜ POST์š”์ฒญ

     

    // Promise ver
    fetch('https://koreanjson.com/users', {
      method: 'POST',
      headers: {
        // JSON์˜ ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด์ค€๋‹ค๊ณ  ์„œ๋ฒ„์—๊ฒŒ ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• ์ž…๋‹ˆ๋‹ค.
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ nickName: 'ApeachIcetea', age: 20 }),
    })
      .then((response) => response.json())
      .then((json) => console.log(json))
      .catch((error) => console.log(error));
    
    // Async / Await ver
    // async function request() {
    //   const response = await fetch('https://koreanjson.com/users', {
    //     method: 'POST',
    //     headers: {
    //       'Content-Type': 'application/json',
    //     },
    //     body: JSON.stringify({ nickName: 'ApeachIcetea', age: 20 }),
    //   });
    //   const data = await response.json();
    //   console.log(data);
    // }
    
    // request();
    
    const appDiv = document.getElementById('app');
    appDiv.innerHTML = `
    <h1>Fetch API ๐Ÿ˜Š</h1>
    <h3>console ์ฐฝ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”! ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡</h3>
    `;

     

    Axios์˜ POST์š”์ฒญ

     

    // axios๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์„ค์น˜ํ•œ axios๋ฅผ ๋ถˆ๋Ÿฌ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    import axios from 'axios';
    
    // Promise ver
    axios
      .post('https://koreanjson.com/users', { nickName: 'ApeachIcetea', age: '20' })
      .then((response) => {
        const { data } = response;
        console.log(data);
      })
      .catch((error) => console.log(error));
    
    // Async / Await ver
    // async function request() {
    //   const response = await axios.post('https://koreanjson.com/users', {
    //     name: 'ApeachIcetea',
    //     age: '20',
    //   });
    //   const { data } = response;
    //   console.log(data);
    // }
    
    // request();
    
    const appDiv = document.getElementById('app');
    appDiv.innerHTML = `
    <h1>Axios โ˜บ๏ธ</h1>
    <h3>console ์ฐฝ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”! ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡</h3>
    `;

     

     

    ๊ฒฐ๊ณผ๋Š” VSCODE๋กœ ํ™•์ธํ•ด๋ณด์ž!

     

    AXOIS๋Š” ์ •๋ง ์ค‘์š”! ๋ฐฑ์—”๋“œ ํ˜‘์—… ์‹œ ์ฃผ๊ตฌ์žฅ์ฐฝ ํ• ๊ฑฐ๋‹ค ๊ผญ ์™ธ์›Œ๋‘์žฅ

     

    ๋ฐ˜์‘ํ˜•

    'codeStates front-end > Java Script' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

    [JS] chapter23. Tree UI  (0) 2023.02.14
    [JS] ์‹ค์Šต - fetch API  (0) 2023.01.19
    [JS] chapter21. ๋น„๋™๊ธฐ3 - fetch API  (0) 2023.01.19
    [JS] chapter20. ๋น„๋™๊ธฐ2 - node.js  (0) 2023.01.19
    [JS] ์‹ค์Šต - ํƒ€์ด๋จธ API  (0) 2023.01.18

    ๋Œ“๊ธ€

Designed by Tistory.