Question Details

No question body available.

Tags

javascript html node.js json express

Answers (1)

Accepted Answer Available
Accepted Answer
October 30, 2025 Score: 1 Rep: 763 Quality: High Completeness: 80%

The first step to solve your problem is to implement some kind of saving. A simple yet stable solution is to utilize the Node's filesystem API. Instead of storing your websites in a variable (which makes them refresh with every session), you should modify the file. As it was already pointed out in the comments, JSON is a good starter for such things. Here is my code (for the sake of simplicity, I used the synchronous variants, you can replace them with async + callback if you wish):

import fs from "fs";

function getWebsites() { // Open the file and read the content const data = fs.readFileSync("./websites.json", "utf-8"); return JSON.parse(data); }

function updateWebsites(name) { const content = getWebsites(); content.websites.push({ name: name, enable: true }); // Write the modified data and close the file fs.writeFileSync("./websites.json", JSON.stringify(content)); }

server.post("/last", (req, res) => { var DataG = req.body.name; updateWebsites(DataG); // ... }

Later you can implement some kind of caching to prevent redundant calls of readFileSync() until something has changed. Moving on to the second part of your idea: you can modify the HTML file with each website list update, but a way more typical way to reflect the data state is to add another endpoint to your server:

server.get("/sites", (req, res) => {
    res.statusCode = 200;
    res.send(getWebsites());
});

Having this set up, the only thing you have to do from your client is to query the list:


    async function displaySites() {

const res = await fetch("http://localhost:3000/sites"); const data = await res.json(); const container = document.getElementById("somecontainer");

data.websites.forEach(el => { const paragraph = document.createElement("p"); paragraph.innerText = el.name; container.appendChild(paragraph); }); }

Display websites