=> Create Read Update Delete
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World')
})
app.listen(8080, () => {
console.log('server started!')
});
const express = require('express')
const cors = require('cors')
const app = express()
app.use(cors())
app.use(express.json());
let id = 2;
const todoList = [{
id: 1,
text: '할일 1',
done: false,
}];
app.get('/', function (req, res) {
res.send('Hello World')
});
app.get('/api/todo', (req, res) => {
res.json(todoList);
})
app.post('/api/todo', (req, res) => {
const { text, done } = req.body;
todoList.push({
id: id++,
text,
done,
});
return res.send('success');
});
app.listen(8080, () => {
console.log('server started!');
});
{
"text": "todo 2",
"done": true
}
import './App.css';
import { useState } from 'react';
function App() {
return (
<div className="App">
<h1>TODO LIST</h1>
</div>
);
}
export default App;
-> fetch만 사용하면 런타임 에러 발생 !!!
import './App.css';
import { useEffect, useState } from 'react';
function App() {
const [ todoList, setTodoList ] = useState(null);
useEffect(() => {
// 자바스크립트에 내장된 api를 사용할 수 있는 함수
fetch('http://localhost:8080/api/todo')
.then((response) => response.json())
.then((data) => setTodoList(data));
}, []);
return (
<div className="App">
<h1>TODO LIST</h1>
</div>
);
}
export default App;