-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
69 lines (52 loc) · 1.84 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const fs = require('fs');
const express = require('express');
const app = express();
app.use(express.json());
app.get('/tasks', (req, res) => {
const tasks = JSON.parse(fs.readFileSync('tasks.json', 'utf8'));
res.json(tasks);
});
app.get('/columns', (req, res) => {
const columns = JSON.parse(fs.readFileSync('columns.json', 'utf8'));
res.json(columns);
});
// Handle POST request for adding new tasks
app.post('/tasks', (req, res) => {
const newTask = req.body;
// Read existing tasks
const tasks = JSON.parse(fs.readFileSync('tasks.json', 'utf8'));
// Add the new task
tasks.push(newTask);
// Save updated tasks to tasks.json
fs.writeFileSync('tasks.json', JSON.stringify(tasks, null, 2));
res.sendStatus(200);
});
// Handle DELETE request for deleting tasks
app.delete('/tasks/:id', (req, res) => {
const taskId = parseInt(req.params.id);
// Read existing tasks
const tasks = JSON.parse(fs.readFileSync('tasks.json', 'utf8'));
// Remove task from tasks
const updatedTasks = tasks.filter(task => task.id !== taskId);
// Save updated tasks to tasks.json
fs.writeFileSync('tasks.json', JSON.stringify(updatedTasks, null, 2));
res.sendStatus(200);
});
app.patch('/tasks/:id', (req, res) => {
const taskId = parseInt(req.params.id);
const newColumn = req.body.column;
// Read existing tasks
const tasks = JSON.parse(fs.readFileSync('tasks.json', 'utf8'));
// Find and update the task
const task = tasks.find(task => task.id === taskId);
if (task) {
task.column = newColumn;
// Save updated tasks to tasks.json
fs.writeFileSync('tasks.json', JSON.stringify(tasks, null, 2));
res.sendStatus(200);
} else {
res.status(404).send('Task not found');
}
});
app.use(express.static('public'));
app.listen(3000, () => console.log('Kanban board running on http://localhost:3000'));