-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
66 lines (52 loc) · 1.75 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
// jshint version:6
const express = require("express");
const bodyParser = require("body-parser");
const date = require(__dirname+"/date.js");
const app = express();
// Array- items assigned newListItems and so it is used in EJS File for <ul>
const items= ["Buy Groceries", "Arriving Guests", "Morning Jog"];
const workItems = [];
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname+"/public"));
app.get("/", (req,res)=>{
const day = date.getDate(); // using the built module of date
// for using the day-module
// let day = date.getDay();
// lists is the name of the EJS file
res.render("lists", {
listTitle : day, // listTitle is a variable that will be called in EJS
newListItems : items // redirect from the post request and return the array of items
});
});
app.post(("/"),(req,res)=>{
// console.log(req.body);
const item = req.body.newItem;
// so if the item is added in work route then it will be pushed in the array workItems
// also the name list is from the button name="list" in our ejs file
if(req.body.list === "Work"){
workItems.push(item);
res.redirect("/work");
}
else{
items.push(item);
res.redirect("/"); // it will redirect to the home route which then triggers the app.get method
}
});
app.get("/work",(req,res)=>{
res.render("lists", {
listTitle:"Work List",
newListItems: workItems
});
});
// app.post("/work",(req,res)=>{
// let item = req.body.newItem;
// workItems.push(item);
// res.direct("/work");
// });
app.get("/about",(req,res)=>{
res.render("about");
});
app.listen(3000,()=>{
console.log("Server started at port 3000.");
});