the html file is written in the file having extension as ejs rather than HTML because it can include embedded javascript logic inside the file itself.
html of ejs todo list:-
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>To do List</title>
</head>
<body>
<% if(kindOfDay==="Saturday"||kindOfDay==="Sunday"){ %>
<h1 style="color:Purple"> <%= kindOfDay %> </h1>
<% } else { %>
<h1 style="color:Blue"> <%= kindOfDay %> </h1>
<% } %>
<ul>
<% for (var i = 0; i<newListItems.length; i++){ %>
<li> <%= newListItems[i] %> </li>
<% } %>
</ul>
<form class="" action="/" method="post">
<input type="text" name="newItem">
<button type="submit" name="button">Add Item</button>
</form>
</body>
</html>
Java Script of ejs todo list:-
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.set('view engine', 'ejs'); //incorporates ejs with express
app.use(express.urlencoded({extended:true})); // express is used as body parse is depreceated
var items = ["BBuy Food","Cook Food","Eat Food"];
app.get("/", (req, res) => {
var today = new Date(); //variable containing new date
var options={ // variable containing what of the date is shown and in which data type
weekday:"long",
day:"numeric",
month:"long",
year:"numeric"
}
var day = today.toLocaleDateString("en-US",options); //converts the date to render the string representation of the date
res.render("list", { // renders the template and sends the rendered html string to client
kindOfDay: day, // item to render
newListItems:items
})
})
app.post("/",(req,res)=>{
var item= String(req.body.newItem); //used to grab the data from input
items.push(item); // pushes grabbed item to items array
res.redirect("/"); // redirects to home route
})
app.listen(3000, () => {
console.log("Server started at port 3000");
})
// var currentDay = today.getDay();
// var day = "";
// switch (currentDay) {
// case 0:
// day = "Sunday"
// break;
// case 1:
// day = "Monday"
// break;
// case 2:
// day = "Tuesday"
// break;
// case 3:
// day = "Wednesday"
// break;
// case 4:
// day = "thursday"
// break;
// case 5:
// day = "Friday"
// break;
// case 6:
// day = "Saturday"
// break;
// default:
// console.log("Error current day is equal to:" + currentDay);
//
// }
Comments
Post a Comment