I'm struggling to comprehend the query you've presented.
Below is a snippet of code that showcases how axios and JavaScript can be utilized together to retrieve data from the backend and allow for modifications on the frontend. Feel free to substitute axios with fetch for similar functionality.
app.js
const express = require("express");
const bodyParser = require("body-parser");
const port = 8000;
const app = express();
/* Simulating data, although utilizing a storage solution like MySQL would be ideal */
let data = {
name: "Elon Musk",
age: "48",
height: "1.88m"
};
app.use(express.static("public"));
/* body-parser is necessary for the server to parse incoming data */
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
/* endpoint to fetch data */
app.get("/api/mydata", function(req, res) {
return res.json(data);
});
/* endpoint where the client posts updated data */
app.post("/api/newdata", function(req, res) {
data.name = req.body.name;
data.age = req.body.age;
return res.json("OK!");
});
app.listen(port, function() {
console.log("Listening on port 8000");
});
public/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="text" id="name" placeholder="Name..." value="">
<input type="text" id="age" placeholder="Age..." value="">
<button type="button" id="setValues">Change values!</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.js"></script>
<script>
window.addEventListener("load", function() {
axios.get("/api/mydata").then(function(res) {
document.getElementById("name").value = res.data.name;
document.getElementById("age").value = res.data.age;
})
});
document.getElementById("setValues").addEventListener("click", function() {
axios.post("/api/newdata", {
name: document.getElementById("name").value,
age: document.getElementById("age").value
}).then(function(res) {
console.log("Data Sent!");
})
})
</script>
</body>
</html>
If there are any uncertainties, please don't hesitate to reach out!