mysqlํ๋ก๊ทธ๋จ์ ์ปดํจํฐ๋ฅผ ํค๋ฉด ๋ฐ๋ก ๋์ํ๊ณ ์์(3306๋ฒ port)
express 3002๋ฒ port
react 3001๋ฒ port
concurrently ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์นํ์ฌ
package.json์ start ๋ช
๋ น์ด๋ฅผ ๋ ๋ค ์คํํ ์ ์๋๋ก ์ค์ ํ์ฌ
npm start๋ฅผ ํ๋ฉด ๋์์ ์๋ฒ์ ํด๋ผ์ด์ธํธ๊ฐ ์ผ์ง๊ฒ ์ค์
๊ฐ์ ์ปดํจํฐ ์์์ ์๋ก ๋ค๋ฅธ ํ๋ก๊ทธ๋จ์ด
๋ค๋ฅธ ํฌํธ์์ ๋์ํ๋ ํ๋ก๊ทธ๋จ์ ์ ๊ทผ์ ํ๋ ค๋ฉด
CORS ์ ์ฑ
์ ์์ ํด์ค์ผ ํ๋ค.
http-proxy-middleware
๋ผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์น
setupProxy.js ํ์ผ์ ๋ง๋ค๊ณ ,
์ฃผ์ ๋์ '/api' ๋ก ๋๋๋ฉด
์๋ฒ ํฌํธ์ชฝ์ผ๋ก ๋ณด๋ด๊ฒ ์ค์ ์ ๋ง๋ค์ด์ค๋ค
localhost:3001/api ๋ก get
์ค์ง์ ์ผ๋ก๋
localhost:3002/api ๋ก get ์์ฒญ์ด ๊ฐ๋ฒ๋ฆฐ๋ค
--> react์์๋ ์ฃผ์ ๋์ api๋ก ๋๋๋ ์ฃผ์๋ ์ฌ์ฉํ ์ ์๋ค
์ ๋ A ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์์ต๋๋ค.
const {createProxyMiddleware} = require('http-proxy-middleware');
module.exports = function(app){
app.use(
createProxyMiddleware('/api', {
target: 'http://localhost:3002',
changeOrigin:true
})
)
}
cors ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํ๋ค
npm install cors
app.use(cors({origin: 'http://localhost:3001'}));
ํด๋น ์ฝ๋ ํ์ค ์ถ๊ฐ
(3001๋ฒํฌํธ ํ์ฉํ๊ฒ ๋ค๋ ์๋ฏธ)
๋ฆฌ์กํธ์์๋ ์์ฒญํ ๋
localhost:3002 get์์ฒญ ์ง์ ์ ์ผ๋ก ํด๋ ์๊ด์์
express react axios
res.json('๋ฐฐ์์ฝ') --> res.data์ '๋ฐฐ์์ฝ'์ด ๋ค์ด์์
res.json(10) --> res.data์ 10์ด ๋ค์ด์์
res.json([1,2,3]) --> res.data์ [1,2,3]์ด ๋ค์ด์์
npm install concurrently --save
concurrently๋ JavaScript ํ๋ก์ ํธ์์ ์ฌ๋ฌ ๊ฐ์ ๋ช ๋ น์ด๋ฅผ ๋ณ๋ ฌ๋ก ์คํํ๋๋ก ๋์์ฃผ๋ ํจํค์ง์ ๋๋ค. (ChatGPT)
npm install http-proxy-middleware --save
์ค์น๊ฐ ์๋ฃ๋๋ฉด ํด๋น ํจํค์ง๋ฅผ require ๋๋ import ๋ฌธ์ ์ฌ์ฉํ์ฌ JavaScript ์ฝ๋์์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ด ํจํค์ง๋ฅผ ์ฌ์ฉํ๋ฉด ์น ์๋ฒ์์ HTTP ํ๋ก์๋ฅผ ์ค์ ํ๋ ๋ฐ ๋์์ด ๋๋ฉฐ, ๋ค๋ฅธ ์๋ฒ๋ก ์์ฒญ์ ์ ๋ฌํ๊ฑฐ๋ ๊ฒฝ๋ก๋ฅผ ๋ฆฌ๋ผ์ฐํ ํ ์ ์์ต๋๋ค. (ChatGPT)
npm install mysql2 --save
"scripts": {
"client": "set PORT=3001 && react-scripts start",
"server": "cd server && nodemon app",
"start": "concurrently --kill-others-on-fail \"npm run server\" \"npm run client\"",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
{
"name": "test",
"version": "0.1.0",
"private": true,
"dependencies": {
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@mui/icons-material": "^5.14.3",
"@mui/material": "^5.14.5",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^1.5.0",
"concurrently": "^8.2.1",
"express": "^4.18.2",
"http-proxy-middleware": "^2.0.6",
"mysql2": "^3.6.1",
"nodemon": "^3.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.15.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"client": "set PORT=3001 && react-scripts start",
"server": "cd server && nodemon app",
"start": "concurrently --kill-others-on-fail \"npm run server\" \"npm run client\"",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
const express = require('express');
// import express from "express"; ์์ฆ ๋ฌธ๋ฒ๊ณผ ๋์ผํ๋ฐ ๋ฐ๋ก ๋ญ ๊น์์ค์ผ ์ด๊ฑธ ์ธ ์ ์์
const mysql = require('mysql2');
// const cors = require('cors');
// mysql ์ค์
const pool = mysql.createPool({
host:'localhost',
user:'user',
database:'board',
password:'1111',
port:'3306'
});
const app = express();
const port = 3002;
// app.use(cors({origin: 'http://localhost:3001'}));
app.get('/api', (req, res)=>{
res.send('์ฑ๊ณตํ์ต๋๋ค!');
})
// ๋ชจ๋ ์ฌ์ ์กฐํ
app.get('/api/employees', (req, res)=>{
// mysql์์ employees ํ
์ด๋ธ ๋ชจ๋ ํ, ์ปฌ๋ผ ์กฐํ
pool.query('select * from employees', (err, rows, fields)=>{
console.log(rows);
// console.log(fields); ์ปฌ๋ผ์ ๋ํ ์ ๋ณด๋ผ ์ค์ง์ ์ผ๋ก ์ ์ฐ์ง ์๋๋ค
res.json(rows);
});
// res.json();
});
// ์ฌ์ ํ๋ช
์ถ๊ฐ
app.post('/api/employees', ()=>{});
app.listen(port, () =>{console.log(`express ์๋ฒ ์คํ๋จ! ํฌํธ:${port}`)});
import axios from "axios";
import { useRef, useState } from "react";
const ExpressTestPage = ()=>{
const [employees, setEmployees] = useState([]);
const onExpressClick = ()=>{
axios.get('/api')
.then((res)=>{
console.log('.์๋ต ๋ฐ์์ด!!', res);
}).catch((err)=>{ console.log('์คํจ!!', err)});
}
const getEmployees = ()=>{
axios.get('/api/employees').then((res)=>{
console.log(res.data);
setEmployees(res.data);
}).catch((err)=>{console.log(err)});
}
const fetchEmployees =()=>{
fetch('/api/employees')
.then((res)=>{return res.json})
.then((data)=>{console.log(data)})
.catch((err)=>{console.log(err)});
}
const empNameRef = useRef(null); // ์ด๊ธฐ๊ฐ์ ๋ฃ์ด๋๋ค
const addEmp= ()=>{
// document.querySelector('#emp_name')
// document.querySelector๋ฅผ ํตํด ํ๊ทธ๋ฅผ ๊ฐ์ ธ์ค๋ ๊ฒ์ด ์๋
// Ref ๊ฐ์ฒด๋ฅผ ํตํด ํ๊ทธ๋ฅผ ๊ฐ์ ธ์จ๋ค
console.log(empNameRef.current.value);
}
return(
<>
<h1>์
๋ ฅ</h1>
<label>
์ฌ์์ด๋ฆ
<input ref={empNameRef} id="emp_name"/>
</label>
<label>
์ฌ์ ๋ด๊ธ
<input id="salary"/>
</label>
<button onClick={addEmp}>์ถ๊ฐ ํ๊ธฐ</button>
<h1>์ฌ์๋ชฉ๋ก</h1>
{employees.map((e)=><div key={e.id}>์ฌ์์ด๋ฆ : {e.emp_name}, ์ฌ์๋ฒํธ : {e.id}, ๋ด๊ธ : {e.salary}</div>)}
{/* <div>์ฌ์์ด๋ฆ: ํ๊ธธ๋, ์ฌ์๋ฒํธ: 1, ๋ด๊ธ: 1000</div>
<div>์ฌ์์ด๋ฆ: ํ๊ธธ๋, ์ฌ์๋ฒํธ: 1, ๋ด๊ธ: 1000</div>
<div>์ฌ์์ด๋ฆ: ํ๊ธธ๋, ์ฌ์๋ฒํธ: 1, ๋ด๊ธ: 1000</div>
<div>์ฌ์์ด๋ฆ: ํ๊ธธ๋, ์ฌ์๋ฒํธ: 1, ๋ด๊ธ: 1000</div>
<div>์ฌ์์ด๋ฆ: ํ๊ธธ๋, ์ฌ์๋ฒํธ: 1, ๋ด๊ธ: 1000</div> */}
<h1>์ต์คํ๋ ์ค ํต์ ํ
์คํธ ํ์ด์ง ์
๋๋ค!</h1>
<button onClick={onExpressClick}>๋ฒํผ์
๋๋ค!</button>
<button onClick={getEmployees}>mysql์์ ๊ฐ์ ธ์ ๋ณด๊ฒ ์ต๋๋ค</button>
<button onClick={fetchEmployees}>fetch๋ก ๋ชจ๋ ์ง์ ์ ๋ณด๋ณด๊ธฐ</button>
</>
);
}
export default ExpressTestPage;
๋์ ํฌ๋ง ๋ด๊ธ์ด๋ค...ใ
ใ
ใ
์๋ต์ ์ ๋ฐ์์ค๋๊ฒ์ ๋ณผ ์ ์๋ค.
IntelliJ ์ Oracle์ ์ด์ฉํด์ CRUD๋ฅผ ํด๋ดค์๋๋ฐ(๋ฐฑ์๋)...
ํ๋ก ํธ์๋ ๋ง์ผ๋ก๋ ์ด๊ฒ ๊ฐ๋ฅํด์ง๋ค๋ ๋๋ฌด ์ ๊ธฐํ๊ณ ๋ ๊ฐํธํ๊ฒ ๊ฐ๋ค๐ฅฐ๐ฅฐ๐ฅฐ