-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
79 lines (63 loc) · 3 KB
/
server.js
File metadata and controls
79 lines (63 loc) · 3 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//Import necessary modules
const express = require('express'); //Web framework for building APIS
const path = require('path'); //Helps with file paths
const fs = require('fs-extra'); //File handling (read/write JSON)
const crypto = require('crypto'); //For generating unique IDs
//Create express app and define port
const app = express();
const PORT = 3000;
//Middleware: serves static files from 'public' folder (HTML, CSS, JS)
app.use(express.static(path.join(__dirname, 'public')));
//Middleware: enables reading JSON data in request body
app.use(express.json());
//Define path to tasks.json file
const tasksFilePath = path.join(__dirname, 'data', 'tasks.json');
//GET endpoint: retrieves all tasks
app.get('/api/tasks', async (req, res) => {
const tasks = await fs.readJson(tasksFilePath); //Read tasks.json file asyncron
res.json(tasks); //Send tasks back as JSON to frontend
});
//POST endpoint: adds a new task
app.post('/api/tasks', async (req, res) => {
const { text } = req.body; //Extract task text from request body
if (!text || text.trim() === '') {
return res.status(400).json({ error: 'Task text is required '})
}
const id = crypto.randomUUID(); //Generate a unique ID using crypto
const tasks = await fs.readJson(tasksFilePath); //Read existing tasks from file
//Create a new task object
const newTask = {
id: id, //Unique ID
text: text, //Task text
completed: false, //Starts as not completed
};
tasks.push(newTask); //Add the new task to the array
await fs.writeJson(tasksFilePath, tasks); //Save the updated array back to the file
res.json(newTask); //Send the new task back to frontend
});
//DELETE endpoint: deletes a task based on ID
app.delete('/api/tasks/:id', async (req, res) => {
const { id } = req.params; //Extract ID from URL parameter (:id in the route)
const tasks = await fs.readJson(tasksFilePath); //Read all tasks
const updatedTasks = tasks.filter(task => task.id !== id); //Filter tasks, keep only thos where ID doesnt match
await fs.writeJson(tasksFilePath, updatedTasks); //Save the updated list (without deleted task)
res.json({success: true}) //Send success message back
});
//PATCH endpoint: Toggles completed status on a task
app.patch('/api/tasks/:id', async (req, res) => {
const { id } = req.params; //Extract ID from URL
const tasks = await fs.readJson(tasksFilePath); //Read all tasks
const task = tasks.find(task => task.id === id); //Find specific task with the given ID
//If task not found, return 404 error
if (!task) {
return res.status(404).json({ error: 'Task not found' });
}
//Toggle completed status (true -> false, false -> true)
task.completed = !task.completed;
await fs.writeJson(tasksFilePath, tasks); //Save the updated list
res.json(task); //Send updated task back
})
//Start the server and listen on port 3000
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});