-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
49 lines (34 loc) · 1.62 KB
/
app.js
File metadata and controls
49 lines (34 loc) · 1.62 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
const express = require("express");
const bodyParser = require("body-parser");
const { read } = require("fs");
const date = require(__dirname + '/date.js');
const app = express();
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static("public")); // express doesn't incorporate all the files in your directory while creating the server but only the .js file and the views directory. So we put all the files in a folder named 'public' and this is how we tell the express to incorporate the files in the 'public'
var itemNames=["buy food", 'cook food', 'eat food'];
let workList= [];
app.set('view engine', 'ejs'); // this is used to incorporate the EJS into our server.js file. there must be a file named 'views' in the same directory where the EJS will go and search for what has to be done
app.get("/", function(req, res){
let day = date.getDate();
res.render("index", {dayName:day, itemNamesList:itemNames}); // the file 'index' must be present in the 'views' directory and it should contain the same html code which you want to display
});
app.post('/', function(req, res){
console.log("this post was from the button:" + req.body.btn)
if(req.body.btn ==='Work')
{
let itemName = req.body.todoItem
workList.push(itemName)
res.redirect('/work');
}
else{
let itemName = req.body.todoItem;
itemNames.push(itemName);
res.redirect('/');
}
});
app.get('/work', function(req, res){
res.render('index', {dayName: "Work List", itemNamesList: workList})
})
app.listen(3000, function(){
console.log("the server is live at port 3000")
})