Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
56 lines (48 loc) · 1.51 KB
/
Copy pathapp.js
File metadata and controls
56 lines (48 loc) · 1.51 KB
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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
let tasks = [];
// Get all tasks
app.get('/tasks', (req, res) => {
res.json(tasks);
});
// Get a single task
app.get('/tasks/:id', (req, res) => {
const task = tasks.find(t => t.id === parseInt(req.params.id));
if (!task) {
return res.status(404).json({ error: 'Task not found' });
}
res.json(task);
});
// Create a new task
app.post('/tasks', (req, res) => {
const { title, description, status, dueDate } = req.body;
const newTask = { id: tasks.length + 1, title, description, status, dueDate };
tasks.push(newTask);
res.status(201).json(newTask);
});
// Update a task
app.put('/tasks/:id', (req, res) => {
const taskIndex = tasks.findIndex(t => t.id === parseInt(req.params.id));
if (taskIndex === -1) {
return res.status(404).json({ error: 'Task not found' });
}
const { title, description, status, dueDate } = req.body;
const updatedTask = { id: tasks[taskIndex].id, title, description, status, dueDate };
tasks[taskIndex] = updatedTask;
res.json(updatedTask);
});
// 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).json({ error: 'Task not found' });
}
tasks.splice(taskIndex, 1);
res.sendStatus(204);
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});