#7. ํ”„๋ก ํŠธ์—”๋“œ ํ™”๋ฉด ๊ฐœ๋ฐœ

 

#7. ํ”„๋ก ํŠธ์—”๋“œ ํ™”๋ฉด ๊ฐœ๋ฐœ

1. React๋กœ ํ™”๋ฉด ๊ฐœ๋ฐœ ์ž, ์ด์ œ๋ถ€ํ„ฐ REACT๋กœ ํ™”๋ฉด์„ ๊ฐœ๋ฐœ ํ•ด๋ณด์ž. ์•ž์—์„œ ์‚ฌ์ „ ์ค€๋น„์‹œ ํ”„๋กœ์ ํŠธ ํด๋”์ธ linkservice์•„๋ž˜์— npx create-react-app ์œผ๋กœ ์ด๋ฏธ client์„ ์ƒ์„ฑํ–ˆ๋‹ค. #4. ํ”„๋กœ์ ํŠธ ํด๋” ๊ตฌ์กฐ ๋ฐ ํ™˜๊ฒฝ์„ค

firstvalue.tistory.com

 

1.1. Component ์ œ์ž‘ ( React๋กœ API ํ˜ธ์ถœ )

์ด์ œ๋ถ€ํ„ฐ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ•˜๋‚˜์”ฉ ์ž‘์„ฑํ•ด ๋ณด์ž.

React์—์„œ Axios๋ฅผ ์ด์šฉํ•ด API ํ˜ธ์ถœํ•˜๊ธฐ

์ปดํฌ๋„ŒํŠธ๋Š” ํฌ๊ฒŒ 3๊ฐ€์ง€๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ž‘์„ฑํ–ˆ๋‹ค.

1.    API URL์„ ๊ด€๋ฆฌํ•˜๋Š” api.config.js

2.    AXIOS๋กœ API์„ ํ˜ธ์ถœํ•˜์—ฌ ๋ฆฌํ„ด ๋ฐ›๋Š” service.js

3.    service์„ ๋ฐ›์•„ ํ™”๋ฉด์— ์ถœ๋ ฅํ•ด์ฃผ๋Š” ์ปดํฌ๋„ŒํŠธ๋“ค

 

๋””๋ ‰ํ† ๋ฆฌ๋Š” src ๋ฐ‘์— /component ์— /api ์™€ /service ๋กœ ๋‚˜๋ˆˆ๋‹ค.

 

โŒ˜ api.config.js ( ์œ„์น˜: linkservice/client/src/component/api/api.config.js)

----------------------------------------------------------------------------------------------

Python Server์—์„œ ๊ฐœ๋ฐœํ•œ API ํ˜ธ์ถœ URL์„ ๊ด€๋ฆฌํ•œ๋‹ค. 

API๊ฐ€ ๋งŽ์•„์ง€๋ฉด ๋ณ„๋„ ๊ด€๋ฆฌ ํ•˜๋Š”๊ฒŒ ์œ ์ง€๋ณด์ˆ˜์ƒ ์ข‹๋‹ค.

 

export const API = {
    //LINKS ์„œ๋น„์Šค API  
    LINKSALL: `/api/links`,
    LINKREGISTER: `/api/link/create`,
    LINKSELECT: `/api/link/select/`,
    LINKDELETE: `/api/link/delete/`,
    LINKUPATE: `/api/link/update`,
}

โŒ˜ service.js ( ์œ„์น˜: linkservice/client/src/component/service/service.js)

------------------------------------------------------------------------------------------------

service.js๋Š” ๊ฐ API ํ˜ธ์ถœ์„ ๋‹ด๋‹นํ•˜๋Š” ํ•จ์ˆ˜๋“ค์„ ๋ชจ์•„๋‘” ์„œ๋น„์Šค๋กœ,

axios ๋กœ API์„ ํ˜ธ์ถœํ•˜์—ฌ ๊ฐ’์„ return๋ฐ›๋„๋ก ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ณ , ์™ธ๋ถ€์—์„œ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋„๋ก linkService๋กœ ์ง€์ •ํ•œ๋‹ค.

import axios from "axios"
import { API } from "../api/api.config"

// ####### ๋งํฌ์ •๋ณด API ํ˜ธ์ถœ
const getLinks = () => {
    return axios.get(API.LINKSALL)
}
const getSelectLink = (id) => {
    return axios.get(API.LINKSELECT + id)
}
const createLink= (values) => {
    return axios.post(API.LINKREGISTER, values)
}
const updateLink = (id, values) => {
    return axios.put(API.LINKUPATE + id, values)
}
const deleteLink = (id) => {
    return axios.put(API.LINKDELETE + id)
}
const linkService = {
    getLinks,
    getSelectLink,
    createLink,
    updateLink,
    deleteLink,
}
export default linkService;

1.2.  ํ™”๋ฉด ์ถœ๋ ฅ

ํ™”๋ฉด ์†Œ์Šค๋Š” .jsx ํ™•์žฅ์ž๋กœ ์ƒ์„ฑํ–ˆ๋‹ค.

 

โŒ˜ links.jsx ( ์œ„์น˜: linkservice/client/src/component/links.jsx)

--------------------------------------------------------------------------------------------------

component์•ˆ์— API์„ ํ˜ธ์ถœํ•˜์—ฌ ๋ฆฌํ„ด ๋ฐ›์€ ๊ฐ’์„ ํ™”๋ฉด์— ์ถœ๋ ฅํ•ด๋ณด์ž.

useEffect์„ ํ†ตํ•ด linkService์˜ getLinks(์ „์ฒด๋งํฌ๋ชฉ๋ก์กฐํšŒ)์„ ํ˜ธ์ถœํ•˜๋Š” ๋กœ์ง์ด๋‹ค.

rendering๋˜๋Š” return๊ฐ’์— <></> Fragment์ถ•์•ฝ์œผ๋กœ ๊ฐ์‹ธ ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜๊ธฐ ๋ฐ”๋ž€๋‹ค.

 

import React, { useState, useEffect } from "react"
import linkService from "./service/service"

export default function LinkMain() {
    const [data, setData] = useState([])

    useEffect(() => {
        linkService.getLinks().then(
        (res)=> {
            setData(res.data)
           console.log(res.data)
        }, (error) => {
           alert(error.res.data.message )
        }
        )
    }, [])
    
    return (
<> 
     <div>
       { data.links && data.links.map((links) => (

        <div key={links.id}> 
            <div >id : {links.id}</div>
            <div>name : {links.name}</div>
            <div>tag : {links.tag}</div>
            <div>imageurl : {links.imageurl}</div>    
            <div>linkurl : {links.linkurl}</div>   
        </div>
                                                              
        ))}
     </div>
 </> 
    ) // return
} // function

 

links.jsx์˜ LinkMain()์„ App.js์—์„œ Router์„ ์ง€์ •ํ•˜์—ฌ ํ˜ธ์ถœํ•ด๋ณด์ž.

 

โŒ˜ App.js ( ์œ„์น˜: linkservice/client/src/App.js)

-------------------------------------------------------------------------------------------------------

import React from "react"
import { BrowserRouter, Route, Routes } from "react-router-dom"
import LinkMain from "./component/links"

function App() {
  return (
    <BrowserRouter>    
      <Routes>
        <Route exact path="/" element={<LinkMain />} />  
      </Routes> 
    </BrowserRouter>
  );
}
export default App;

 

ํ„ฐ๋ฏธ๋„์„ ์—ด์–ด์„œ yarn start์„ ์‹คํ–‰ํ•ด๋ณด์ž. ํ•˜์ง€๋งŒ, ๋ชจ๋“ˆ ์˜ค๋ฅ˜๊ฐ€ ๋‚ ๊ฒƒ์ด๋‹ค.

๊ด€๋ จ ๋ชจ๋“ˆ์„ ์„ค์น˜ ์•ˆ ํ•ด์„œ ๋‚˜๋Š” ๊ฒƒ์œผ๋กœ ์šฐ์„  ๋ชจ๋“ˆ ์„ค์น˜๋ฅผ ํ•˜์ž.

 

$ yarn add react-router-dom

$ yarn add axios

 

$yanr start

 

๋ชจ๋“ˆ ์„ค์น˜ ํ›„ ์‹คํ–‰ํ•˜๋ฉด ๋นˆ ํ™”๋ฉด์— ๋ธŒ๋ผ์šฐ์ € ๊ฐœ๋ฐœ์ž ๋ชจ๋“œ์— ๋ณด๋ฉด 404์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

ํฌ๋กฌ์—์„œ ๊ฐœ๋ฐœ์ž๋„ ๋ชจ๋“œ์—์„œ ํ™•์ธ

์˜ค๋ฅ˜ Url์„ ๋ณด๋ฉด API ํ˜ธ์ถœ ์‹œ ๋ฐœ์ƒํ•œ๋‹ค.

React์—์„œ ๋ฐฑ์—”๋“œ ์„œ๋ฒ„๋กœ API ์š”์ฒญ ์‹œ Proxy ์„ค์ •์„ ํ•ด์ค˜์•ผ ์ •์ƒ ํ˜ธ์ถœ์ด ๋œ๋‹ค.

์šฐ๋ฆฌ๋Š” Python Flask๋กœ API์„ ๊ตฌํ˜„ ํ–ˆ์œผ๋ฏ€๋กœ, Python Server๋‹จ์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค.

 

โŽฎProxy ์„ค์ •

 

client์˜ package.json ์—ด์–ด proxy์„ค์ •์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

"proxy": "http://0.0.0.0:5000",

package.json์— proxy์„ค์ •

   ๐Ÿ“Œ ์•„์ดํ”ผ๋ฅผ 127.0.0.1๋กœ ์„ค์ •์•ˆํ•˜๊ณ , 0.0.0.0 ๋กœ ์„ค์ •ํ•œ ์ด์œ ๋Š” ํด๋ผ์šฐ๋“œ ํ™˜๊ฒฝ(AWS, CAFE24๋“ฑ) ๊ฒฝ์šฐ ๋‚ด๋ถ€ IP๊ฐ€ 192.168๋กœ ์‹œ์ž‘๋  ์ˆ˜ ์žˆ์–ด์„œ ๋ชจ๋“  IPv4 ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์„ค์ •ํ•˜๊ณ ์ž 0.0.0.0๋กœ ํ–ˆ๋‹ค.

 

์ด์ œ ์‹คํ–‰ํ•ด๋ณด์ž

โŽฎ์‹คํ–‰๋ฐฉ๋ฒ•

    -       ์„œ๋ฒ„ ( $ linkservice/server ) ํ„ฐ๋ฏธ๋„

          1.    python ๊ฐ€์ƒํ™” ์‹คํ–‰ ( source ./bin/activate )

          2.    python3 server.py ์‹คํ–‰

          3.    POSTMAN์„ ํ†ตํ•ด์„œ ์ƒ˜ํ”Œ ๋ฐ์ดํƒ€๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. 

          โ–บ POST /api/link/create - ๋งํฌ์ •๋ณด ๋“ฑ๋ก ์ฐธ๊ณ 

#6. Python์œผ๋กœ API ๊ฐœ๋ฐœ(API Resource์„ ์–ธ ๋ฐ ํ…Œ์ŠคํŠธ)

 

#6. Python์œผ๋กœ API ๊ฐœ๋ฐœ(API Resource์„ ์–ธ ๋ฐ ํ…Œ์ŠคํŠธ)

#6. Python์œผ๋กœ API ๊ฐœ๋ฐœ(๋ฐ์ดํƒ€๋ฒ ์ด์Šค ๋ชจ๋ธ ์ž‘์—…) #6. Python์œผ๋กœ API ๊ฐœ๋ฐœ(๋ฐ์ดํƒ€๋ฒ ์ด์Šค ๋ชจ๋ธ ์ž‘์—…) #5. Python์œผ๋กœ API ๊ฐœ๋ฐœ(๊ธฐ๋ณธํ™˜๊ฒฝ๊ตฌ์„ฑ) #5. Python์œผ๋กœ API ๊ฐœ๋ฐœ(๊ธฐ๋ณธํ™˜๊ฒฝ๊ตฌ์„ฑ) โŒ˜ server.py ( ์œ„์น˜: linkservice/

firstvalue.tistory.com

 

-       ํด๋ผ์ด์–ธํŠธ ( $ linkservice/client ) ํ„ฐ๋ฏธ๋„

       1.    yarn start ์‹คํ–‰

 

๋ธŒ๋ผ์šฐ์ €์—์„œ http://localhost:3000 ์œผ๋กœ ํ™•์ธํ•ด๋ณด์ž.

์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋จ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

ํฌ๋กฌ ๊ฐœ๋ฐœ์ž๋ชจ๋“œ Console

๐Ÿ“Œ links.jsx ์†Œ์Šค์— API์„ ํ†ตํ•ด JSON์„ ์ œ๋Œ€๋กœ ๋ฐ›์•„์˜ค๋Š”์ง€ ํ™•์ธํ•˜๊ณ ์ž useEffect ๋ถ€๋ถ„์— console.log์„ ์ฐ์–ด ๋ธŒ๋ผ์šฐ์ €

     ๊ฐœ๋ฐœ์ž๋ชจ๋“œ์— console์— {links: Array(1)} ๊ฐ€ ๋ณด์ธ๋‹ค.

 


ํ™”๋ฉด์ด ์ด์˜์ง€ ์•Š์•„, ๋‹ค์Œ ํฌ์ŠคํŒ…์—๋Š” ๋””์ž์ธ์„ ํ•ด๋ณด์ž.

+ Recent posts