So i have a HTML page that asks the user the name, the description and the image of an activity (like a formular). I want to make a function that saves all the user input into the data.json file when the user clicks on the button but i don't know how to do it. Here the function about the button in js :
function send() {
var nomActi = document.getElementById("nomActi").value;
var descriActi = document.getElementById("descriActi").value;
var imageActi = document.getElementById("imageActi").value;
}
Heres how you can write to a json file.
fs - module to write it to a file
const fs = require('fs');
// create a JSON object
const user = {
"id": 1,
"name": "John Doe",
"age": 22
};
// convert JSON object to string
const data = JSON.stringify(user);
// write JSON string to a file
fs.writeFile('user.json', data, (err) => {
if (err) {
throw err;
}
console.log("JSON data is saved.");
});