forked from Pranavnk15/Todo-List
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
94 lines (76 loc) · 2.47 KB
/
app.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const express = require("express");
const bodyParser = require("body-parser");
const date = require(__dirname + "/date.js");
const app = express();
const items = ["Buy Food", "Cook Food", "Eat Food"]; // Initialize arrays to store items and work items
const workItems = [];
app.set("view engine", "ejs"); // Set the view engine to EJS
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public")); // Serve static files from the 'public' directory
// Define a route for the root URL
app.get("/", function (req, res) {
// Get the current date
const day = date.getDate();
// Render the 'list' template
res.render("list", {
listTitle: day,
newListItems: items,
});
});
// Handle POST requests to add new items
app.post("/", function (req, res) {
const item = req.body.newItem;
// Check if the submitted item belongs to the 'work' list
if (req.body.list === "work") {
// Add the item to the workItems array
workItems.push(item);
// Redirect to the '/work'
res.redirect("/work");
} else {
// Add the item to the items array
items.push(item);
// Redirect to the root ("/")
res.redirect("/");
}
});
// Define a route for the "/work" URL
app.get("/work", function (req, res) {
// Render the 'list' template for the work list
res.render("list", { listTitle: "Work List", newListItems: workItems });
});
// Define a route for editing a to-do item
app.get("/edit/:index", function (req, res) {
const index = req.params.index;
// Render the 'edit' template with the item to edit
res.render("edit", {
listTitle: "Edit Item",
index: index,
itemToEdit: items[index],
});
});
// Handle POST requests to save the edited item
app.post("/edit/:index", function (req, res) {
const index = req.params.index;
const updatedItem = req.body.updatedItem;
// Update the item at the specified index
items[index] = updatedItem;
// Redirect to the root ("/")
res.redirect("/");
});
// Define a route for deleting a to-do item
app.post("/delete/:index", function (req, res) {
const index = req.params.index;
// Remove the item at the specified index
items.splice(index, 1);
// Redirect to the root ("/")
res.redirect("/");
});
// Define a route for the "/about" URL
app.get("/about", function (req, res) {
// Render the 'about' template
res.render("about");
});
// Start the server on port 3000 and show a message when it starts
app.listen(3000, function () {
console.log("Server started on port 3000");
});