๐ฏ ํ์ต ๋ชฉํ
- ์ฌ์ฉ์๋ณ ํ ์ผ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฆฌํ์ฌ ์ ์ฅ/์กฐํํ๋ ๊ตฌ์กฐ๋ฅผ ๋ง๋ ๋ค.
- ์ถํ ๋ก๊ทธ์ธ ์ฐ๋์ ๋๋นํ์ฌ ๊ธฐ๋ณธ ์ธ์ฆ ์ค๊ณ๋ฅผ ๊ฐ์ถ๋ค.
- ์ฌ์ฉ์ ๋ฐ์ดํฐ๋ฅผ
todos.json
ํ์ผ์ ์๊ตฌ ์ ์ฅํ๋ค.
โ 1. ์ ์ฌ์ฉ์ ๊ตฌ๋ถ์ด ํ์ํ๊ฐ?
ํ์ฌ ์๋ฒ๋ **๋ชจ๋ ์ฌ์ฉ์๊ฐ ๊ฐ์ ํ ์ผ ๋ชฉ๋ก(todos)**์ ๊ณต์ ํ๊ณ ์์ต๋๋ค.
ํ์ง๋ง ์ค์ ์๋น์ค์์๋ ์๋์ ๊ฐ์ ๊ตฌ์กฐ๊ฐ ํ์ํฉ๋๋ค:
์๊ตฌ์ฌํญ | ์ค๋ช |
---|---|
์ฌ์ฉ์๋ณ ํ ์ผ ๋ถ๋ฆฌ | ๋ค๋ฅธ ์ฌ์ฉ์์ ๋ฐ์ดํฐ๋ฅผ ๋ณผ ์ ์์ด์ผ ํจ |
์์ /์ญ์ ์ ํ | ๋ณธ์ธ์ ๋ฐ์ดํฐ๋ง ์์ ๊ฐ๋ฅํด์ผ ํจ |
์์์ฑ | ์๋ฒ ๊บผ์ก๋ค ์ผ๋ ๊ฐ์์ ํ ์ผ ๋ชฉ๋ก ์ ์ง |
โ 2. ์ฌ์ฉ์ ์๋ณ์(userId) ๊ธฐ๋ฐ ๊ตฌ์กฐ ์ค๊ณ
โ๏ธ ์ฌ์ฉ์ ๊ตฌ๋ถ ๋ฐฉ์
- Vue ํ๋ก ํธ์๋์์ Axios ์์ฒญ ์๋ง๋ค
userId
๋ฅผ ํจ๊ป ์ ๋ฌ - ์๋ฒ์์๋
userId
๋ณ๋ก ๋ฐ์ดํฐ๋ฅผ ๋๋ ์ ์ฅ (todosByUser[userId]
)
โ๏ธ ์ ์ฅ ๊ตฌ์กฐ ์์ (todos.json
)
{
"abc123": [
{ "id": 1, "title": "์ฌ์ฉ์ A์ ํ ์ผ" }
],
"def456": [
{ "id": 2, "title": "์ฌ์ฉ์ B์ ํ ์ผ" }
]
}
โ
3. ์๋ฒ ์ฝ๋: routes/todos.js
(์ต์ข
)
const express = require('express')
const fs = require('fs').promises
const path = require('path')
const router = express.Router()
const DATA_FILE = path.join(__dirname, '../data/todos.json')
let todosByUser = {}
// ๐ ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
const loadTodos = async () => {
try {
const data = await fs.readFile(DATA_FILE, 'utf-8')
todosByUser = JSON.parse(data)
console.log('โ
todos.json ๋ก๋ ์๋ฃ')
} catch {
todosByUser = {}
console.log('๐ ์ todos.json ์์')
}
}
// ๐พ ๋ฐ์ดํฐ ์ ์ฅ
const saveTodos = async () => {
await fs.writeFile(DATA_FILE, JSON.stringify(todosByUser, null, 2))
}
// GET /todos?user=abc123
router.get('/', async (req, res) => {
const userId = req.query.user
if (!userId) return res.status(400).json({ message: 'userId๊ฐ ํ์ํฉ๋๋ค' })
const todos = todosByUser[userId] || []
res.json(todos)
})
// POST /todos
router.post('/', async (req, res) => {
const { title, userId } = req.body
if (!title || !userId) {
return res.status(400).json({ message: 'title๊ณผ userId๊ฐ ํ์ํฉ๋๋ค' })
}
const newTodo = {
id: Date.now(),
title,
completed: false
}
if (!todosByUser[userId]) todosByUser[userId] = []
todosByUser[userId].unshift(newTodo)
await saveTodos()
res.status(201).json(newTodo)
})
// DELETE /todos/:id?user=abc123
router.delete('/:id', async (req, res) => {
const userId = req.query.user
const id = Number(req.params.id)
if (!userId || !todosByUser[userId]) return res.sendStatus(204)
todosByUser[userId] = todosByUser[userId].filter(todo => todo.id !== id)
await saveTodos()
res.sendStatus(204)
})
// PUT /todos/:id
router.put('/:id', async (req, res) => {
const userId = req.body.userId
const id = Number(req.params.id)
const { title } = req.body
if (!userId || !title || !todosByUser[userId]) {
return res.status(400).json({ message: '์ ํจํ์ง ์์ ์์ฒญ์
๋๋ค' })
}
todosByUser[userId] = todosByUser[userId].map(todo =>
todo.id === id ? { ...todo, title } : todo
)
await saveTodos()
res.sendStatus(200)
})
// ์๋ฒ ์์ ์ ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
loadTodos()
module.exports = router
โ 4. ํด๋ผ์ด์ธํธ(Vue)์์ ์์ฒญ ๋ฐฉ์
const userId = 'abc123' // ์ถํ localStorage์์ ๊ด๋ฆฌ ๊ฐ๋ฅ
// GET
axios.get(`/api/todos?user=${userId}`)
// POST
axios.post('/api/todos', { title: this.newTodo, userId })
// DELETE
axios.delete(`/api/todos/${id}?user=${userId}`)
// PUT
axios.put(`/api/todos/${id}`, { title: newTitle, userId })
โ
5. ์ ์ง๋๋ ํ์ผ ์์ (data/todos.json
)
{
"abc123": [
{ "id": 1721383512188, "title": "Vue ํ์ต", "completed": false },
{ "id": 1721383469384, "title": "Express ์ฐ๋", "completed": false }
],
"def456": []
}
๐ง ์์ฝ
ํญ๋ชฉ | ์ค๋ช |
---|---|
์ฌ์ฉ์ ๊ตฌ๋ถ ๋ฐฉ์ | ๋ชจ๋ ์์ฒญ์ userId ํฌํจ |
์๋ฒ ์ ์ฅ ๊ตฌ์กฐ | todosByUser[userId] ๋ก ๋ถ๋ฆฌ |
์์ํ | JSON ํ์ผ(todos.json )๋ก ์ ์ฅ/๋ถ๋ฌ์ค๊ธฐ |
๋ณด์ ์ค๋น | ์ถํ ๋ก๊ทธ์ธ + ์ธ์ฆ ํ ํฐ์ผ๋ก ํ์ฅ ๊ฐ๋ฅ |
FE ์ ์ฅ์
GitHub - heroyooi/vue2-app at ch10
BE ์ ์ฅ์
๐ฌ ๋๊ธ
โป ๋ก๊ทธ์ธ ํ ๋๊ธ์ ์์ฑํ ์ ์์ต๋๋ค.