ํ์ต ํค์๋
Express ์ด์ฉํ ๊ฐ๋จํ Server ๊ตฌ์ถ
Express ์ด์ฉํ ๊ฐ๋จํ Server ๊ตฌ์ถ
ํ์ํ ํจํค์ง ์ค์น
Copy npm init -y
npm i express nodemon uuid
Server ๊ตฌ๋ ๋ฐฉ๋ฒ
ํด๋น ๋ฐฉ์์ ์์ ์ฌํญ์ ๋ฐ์ํด์ฃผ์ง ์์, server off ํ๊ณ ์ฌ๊ตฌ๋
Copy node server.js // ๐๐ป ๊ตฌ๋ํ ํ์ผ
nodemon์ server.js์ ์์ ์ฌํญ์ ๊ฐ์งํด์ ๋ฐ๋ก ๋ฐ์ํด์ฃผ๊ธฐ ๋๋ฌธ์ ์ฌ์ฉ
Copy npx nodemon server.js
๐ API๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์ ์์์ผ ํ๋ ๊ฐ๋
์น๋ธ๋ผ์ฐ์ ์ ์๋ฒ์ ํต์ ํ๊ธฐ์ํด์ HTTP ํ๋กํ ์ฝ์ ์ฌ์ฉ
HTTP Protocol : TCP/IP ๊ธฐ๋ฐ์ผ๋ก ํด๋ผ์ด์ธํธ์ ์๋ฒ ์ฌ์ด์ ์ด๋ฃจ์ด์ง๋ ์์ฒญ/์๋ต ํ๋กํ ์ฝ
TCP/IP ๊ธฐ๋ฐ์ผ๋ก ํด๋ผ์ด์ธํธ์ ์๋ฒ ์ฌ์ด์ ์ด๋ฃจ์ด์ง๋ ์์ฒญ/์๋ต ํ๋กํ ์ฝ
QUIC : UDP ๊ธฐ๋ฐ์ผ๋ก ๋น ๋ฅธ ์ ์ก ์๋๋ฅผ ๊ฐ์ง๋ ํ๋กํ ์ฝ
HTTP Method : GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD, TRACE
GET : ๋ฐ์ดํฐ๋ฅผ ์์ฒญํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๋ฉ์๋ โ ๋ณด์์ ์ฝํจ
POST : ๋ฐ์ดํฐ๋ฅผ ์์ฑํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๋ฉ์๋ โ ๋ณด์์ ์์ ์ฑ ๋์
PUT : ๋ฐ์ดํฐ๋ฅผ ์
๋ฐ์ดํธํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๋ฉ์๋ โ ๋ฐ์ดํฐ๋ฅผ ์ ์ฒด์ ์ผ๋ก
์
๋ฐ์ดํธ
PATCH : ๋ฐ์ดํฐ๋ฅผ ์
๋ฐ์ดํธํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๋ฉ์๋ โ ๋ฐ์ดํฐ๋ฅผ ์ผ๋ถ๋ถ๋ง
์
๋ฐ์ดํธ
DELETE : ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๋ฉ์๋
Http/Https : Https(๋ณด์ ์ธ์ฆ์๊ฐ ์ถ๊ฐ๋ ํ๋กํ ์ฝ) โ ์ธํ๋ผ ์์ญ
๐ค RESTful, REST ํธ์ถํ๋ค๋ผ๋ ์๋ฏธ๋? HTTP Method๋ฅผ ๊ท์น์ ๋ง๋ API ์ค๊ณ ์ ๋ต
๐ฉ๐ปโ๐ป TodoList์ API ์์ฑ
GET : Todos ์กฐํ
Copy let todos = [];
app.get('/todos', (req, res) => {
res.status(200).json(todos);
});
POST : Todo ์์ฑ
Copy const bodyParser = require('body-parser');
const { v4: uuidv4 } = require('uuid');
app.post('/todos', (req, res) => {
const { task } = req.body;
const newTodo = {
id: uuidv4(),
task,
completed: false,
};
res.status(200).json(newTodo);
});
PATCH : Todo Completed Update
Copy app.patch('/todos/:id', (req, res) => {
const { id } = req.params;
const findTodo = todos.find((todo) => todo.id === id);
if (!findTodo) {
res.status(400).send('Not found Todo');
}
todos = todos.map((todo) =>
todo.id === id ? { ...todo, completed: !todo.completed } : todo
);
res.status(200).send(findTodo);
});
PUT : Todo Task Update
Copy app.put('/todos/:id', (req, res) => {
const { id } = req.params;
const { task } = req.body;
const findTodo = todos.find((todo) => todo.id === id);
if (!findTodo) {
res.status(400).send('Not found Todo');
}
todos = todos.map((todo) => (todo.id === id ? { ...todo, task } : todo));
res.status(200).send(todos.find((todo) => todo.id === id));
});
DELETE : ํ ์ผ ์ญ์
Copy app.delete('/todos/:id', (req, res) => {
const { id } = req.params;
const findTodo = todos.find((todo) => todo.id === id);
if (!findTodo) {
res.status(400).send('Not found Todo');
}
todos = todos.filter((todo) => todo.id !== id);
res.status(200).send('Todo Delete Success');
});
Fetch API
CORS
ํด๊ฒฐํ๋ ์ฃผ์ฒด๋ Backend (๊ทผ๋ณธ)
ํ์ต์ ๋ชฉ์ ์์ผ๋ก, Express ๋ฏธ๋ค์จ์ด๋ก ํด๊ฒฐ
Copy // server.js
const cors = require('cors');
app.use(cors());
GET
Copy const fetchDate = () => {
fetch('http://localhost:4000/todos') // ๐๐ป Promise ๋ฐํ
.then((res) => res.json())
.then((data) => console.log(data))
.catch((e) => console.log(e));
};
async/await ๊ณผ ํจ๊ป ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
Copy const fetchDateAsync = async () => {
try {
const response = await fetch('http://localhost:4000/todos');
const data = await response.json();
console.log(data);
} catch (e) {
console.log(e);
}
};
Copy const fetchDateAsync = async () => {
try {
const data = await (await fetch('http://localhost:4000/todos')).json();
console.log(data);
} catch (e) {
console.log(e);
}
};
POST
Copy const fetchAdd = async () => {
await fetch('http://localhost:4000/todos', {
method: 'POST', // ๐๐ป ํญ์ ๋๋ฌธ์๋ก
headers: {
'Content-Type': 'application/json', // ๐๐ป ๊ผญ Data์ ํ์
์ ๋ช
์ํด์ฃผ์ด์ผ ํ๋ค.
},
body: JSON.stringify({ text: 'Learn React' }),
});
};
PATCH
Copy const fetchToggle = async () => {
await fetch(`http://localhost:4000/todos/${id}`, {
method: 'PATCH',
});
};
DELETE
Copy const fetchDelete = async () => {
await fetch(`http://localhost:4000/todos/${id}`, {
method: 'DELETE',
});
};
Axios
๐ง ๋ฉด์ ์ง๋ฌธ : ๋๋ค http ๋ฆฌํ์คํธ๋ฅผ ๋ง๋๋๊ฑด ๋์ผํ๋ค. fetch API ๋ด์ฅ ํจ์์ด๊ณ , axios๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๊ธฐ ๋๋ฌธ์ fetch API ๋ณด๋ค ๋ ๋์ ๋ฒ์๋ฅผ ์ปค๋ฒ ํ ์ ์๋ค. (์ฌ์ฉ์ ์์ด ํธ์์ฑ์ ์ ๊ณตํ๋ ์ฅ์ ์ด ์๋ค.)
GET
Copy const axiosDate = () => {
axios
.get('http://localhost:4000/todos')
.then((res) => {
console.log(res);
console.log(res.data);
})
.catch((e) => console.log(e));
};
Copy const axiosDateAsync = async () => {
try {
const response = await axios.get('http://localhost:4000/todos');
console.log(response.data);
} catch (e) {
console.log(e);
}
};
POST
Copy const axiosPost = async () => {
try {
await axios.post('http://localhost:4000/todos', {
text: 'Learn React Axios',
});
} catch (e) {
console.log(e);
}
};
Patch
Copy const axiosPatch = async () => {
try {
await axios.patch(`http://localhost:4000/todos/${id}`);
} catch (e) {
console.log(e);
}
};
Delete
Copy const axiosDelete = async () => {
try {
await axios.delete(`http://localhost:4000/todos/${id}`);
} catch (e) {
console.log(e);
}
};
Last updated 10 months ago