Student Activity
Student Activity (30-min)
bash코드 복사mkdir task-api cd task-api npm init -y npm install expressjavascript코드 복사const express = require('express'); const app = express(); app.use(express.json()); const tasks = [ { id: 1, title: 'Task 1', completed: false }, { id: 2, title: 'Task 2', completed: true } ]; // GET all tasks app.get('/tasks', (req, res) => { res.json(tasks); }); // POST a new task app.post('/tasks', (req, res) => { const task = { id: tasks.length + 1, ...req.body }; tasks.push(task); res.status(201).json(task); }); // PUT to update a task app.put('/tasks/:id', (req, res) => { const task = tasks.find((t) => t.id === parseInt(req.params.id)); if (!task) return res.status(404).send('Task not found'); Object.assign(task, req.body); res.json(task); }); // DELETE a task app.delete('/tasks/:id', (req, res) => { const taskIndex = tasks.findIndex((t) => t.id === parseInt(req.params.id)); if (taskIndex === -1) return res.status(404).send('Task not found'); tasks.splice(taskIndex, 1); res.status(204).send(); }); const PORT = 3000; app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));bash코드 복사node server.js
json코드 복사{ "title": "New Task", "completed": false }
json코드 복사{ "title": "Updated Task", "completed": true }
Last updated