-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (55 loc) · 1.85 KB
/
server.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
import express from "express";
import { fileURLToPath } from "url";
import { dirname } from "path";
import bodyParser from "body-parser";
const app = express();
const __dirname = dirname(fileURLToPath(import.meta.url));
const port = 3000;
const date = new Date();
let dayt = date.getDate();
let month = date.getMonth();
let day = date.getDay();
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
let year = date.getFullYear();
var workList = [];
var todayList = [];
// Middleware to check and clear the arrays if the day has changed
function checkAndClearArrays(req, res, next) {
const currentDate = new Date();
if (currentDate.getDate() !== dayt) {
workList = [];
todayList = [];
dayt = currentDate.getDate();
month = currentDate.getMonth();
day = currentDate.getDay();
year = currentDate.getFullYear();
}
next();
}
//server JS
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(checkAndClearArrays);
app.get("/", (req,res)=>{
res.render(__dirname +"/views/today.ejs", { dayt, month, day, year, monthnames: months, daynames: days, listItems: todayList });
});
app.get("/work", (req,res)=>{
res.render(__dirname +"/views/work.ejs",{ dayt, month, day, year, monthnames: months, daynames: days, listItems: workList });
});
app.post("/adder",(req,res, next)=>{
var key = Object.keys(req.body)[0];
var ItemToBeAdded = req.body[key];
if (key == "newItem") {
workList.push(ItemToBeAdded);
res.redirect("/work");
next();
} else {
todayList.push(ItemToBeAdded);
res.redirect("/");
next();
}
});
app.listen(port, (req,res)=>{
console.log("Listening to port : " + port);
})