-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
81 lines (73 loc) · 2.21 KB
/
server.js
File metadata and controls
81 lines (73 loc) · 2.21 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
80
81
const express = require("express");
const server = express();
const model = require("./model/tasks.js");
server.use(express.urlencoded({ extended: false }));
server.get("/", (req, res) => {
const tasks = model.listTasks();
const list = tasks.map(Task);
const body = /*html*/ `
<!doctype html>
<form method="POST">
<input id="content" name="content" aria-label="New task" required>
<button>Add task +</button>
</form>
<ul>${list.join("")}</ul>
`;
res.send(body);
});
server.post("/", (req, res) => {
const task = {
content: req.body.content,
complete: 0,
};
model.createTask(task);
res.redirect("/");
});
// server.get("/", (req, res) => {
// const tasks = model.listTasks();
// const body = /*html*/ `
// <!doctype html>
// <form method="POST">
// <input id="content" name="content" aria-label="New task" required>
// <button>Add task +</button>
// </form>
// <ul>${tasks.map((t) => `<li>${t.content}</li>`).join("")}</ul>
// `;
// res.send(body);
// });
server.post("/update", (req, res) => {
const { action, id } = req.body;
if (action === "remove") model.removeTask(id);
if (action === "toggle") model.toggleTask(id);
res.redirect("/");
});
function Task(task) {
return /*html*/ `
<li>
<form method="POST" action="/update">
<input type="hidden" name="id" value="${task.id}">
<button name="action" value="toggle" aria-label="Toggle complete">
${task.complete ? "☑︎" : "☐"}
</button>
<span style="${task.complete ? "text-decoration: line-through" : ""}">
${task.content}
</span>
<button name="action" value="remove">×</button>
</form>
</li>
`;
}
// server.get("/", (req, res) => {
// const tasks = model.listTasks();
// const list = tasks.map(Task);
// const body = /*html*/ `
// <!doctype html>
// <form method="POST">
// <input id="content" name="content" aria-label="New task" required>
// <button>Add task +</button>
// </form>
// <ul>${list.join("")}</ul>
// `;
// res.send(body);
// });
module.exports = server;