-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (38 loc) · 1.1 KB
/
index.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
import express from 'express';
import bodyParser from 'body-parser';
const app = express();
const port = 3000;
let task = [];
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.get("/", (req, res) => {
res.render("index", { task: task });
});
app.post('/addtask', (req, res) => {
const newTask = req.body.newtask;
if (newTask.trim() !== '') {
task.push(newTask);
}
res.redirect("/");
});
app.post('/delete-task', (req, res) => {
const taskIndex = Number(req.body.taskIndex);
if (!isNaN(taskIndex) && taskIndex >= 0 && taskIndex < task.length) {
task.splice(taskIndex, 1);
}
res.redirect('/');
});
app.post('/edit-task', (req, res) => {
const taskIndex = Number(req.body.taskIndex);
const newText = req.body.newText;
if (!isNaN(taskIndex) && taskIndex >= 0 && taskIndex < task.length) {
if (newText.trim() !== '') {
task[taskIndex] = newText;
}
}
res.redirect('/');
});
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});