I am working on creating a dynamic todo list where new items can be added using a form. The issue I am facing is that when the page is refreshed, the existing data in the array persists, which is not the desired behavior.
What I want is for the previous data to be cleared every time the page is refreshed and only fresh items to be added to the array. However, currently, I have to restart the server each time to achieve this result.
initial list:
li1
li2
li3
after item addition:
li1
li2
li3
li4
li5
after refresh: (expected)
li1
li2
li3
after refresh: (actual o/p)
li1
li2
li3
li4
li5
Snippet of my code:
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
var Item = ["Home List","Shop List","Bucket List"];
app.use(bodyParser.urlencoded({extended:true}));
app.set("view engine","ejs");
var options = {
weekday:"long",
day:"numeric",
month:"long"
};
app.get("/", function (req, res) {
var today = new Date();
var currentDay = today.toLocaleDateString("en-US",options);
res.render("lists",{
day: currentDay,
newListItem : Item
});
});
app.post("/",function(req,res){
Item.push(req.body.newItem);
res.redirect("/");
});
app.listen(3000, function () {
console.log("Server running on port 3000");
});