我正在嘗試制作一個待辦事項列表,我想在其中使用表單將新項目添加到列表中。該列表工作正常,但是當我刷新頁面時,已經存儲在數組中的數據被保留。我不想要那個。我希望在每次刷新時,舊數據都會丟失,新項目會被推送到數組中。但是每次我必須殺死并重新啟動服務器時都要這樣做。initial list:li1li2li3after item addition:li1li2li3li4li5after refresh: (expected)li1li2li3after refresh: (actual o/p) li1li2li3li4li5我的代碼: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 on port 3000");});
全局數組在我的 JavaScript 程序中保留刷新數據
小怪獸愛吃肉
2022-10-13 15:33:09