Asked
Active
Viewed 405 times
0
Hello, I am currently developing a website for my personal needs for which I need to be able to regularly save data in a "data.json" file, which I can then retrieve for later use.
I then remembered that I had already used the "file sync" module of NodeJs before, but this time I can't manage to use it with my project as it doesn't seem to apply to my html files...
I suspect that there are other easier solutions or that I must be using NodeJs wrong in this case.
I use the sublime text editor and I work on 2 html files linked to the same script.js and style.css file
Would you have solutions to propose to me?
Thank you, cordially,
Florent
-
What do you want to happen? Saving data is something that has been asked about many times before on Stack Overflow. Have you read those questions and found them lacking? – Heretic Monkey Mar 19 '21 at 20:38
-
Does this answer your question? [Writing files in Node.js](https://stackoverflow.com/questions/2496710/writing-files-in-node-js) – Heretic Monkey Mar 19 '21 at 20:40
2 Answers
0
Let's say at the backend.
const htmlTxt = `
<html>
...
</html>
`
// write the file locally
fs.writeFileSync("sampleHtml.html", htmlTxt);
// read the contents of the file
console.log(fs.readFileSync("sampleHtml.html", "utf8"));

KnowYourElements
- 392
- 1
- 12
0
I would use the File system module.
So on your nodeJS server, you would have to add:
const fs = require('fs');
then create your json:
let jsonElement = {
aa: 'aa',
test: 123,
};
let data = JSON.stringify(jsonElement);
And save it to file
fs.writeFileSync('json_file.json', data);

Piotr Kolecki
- 360
- 1
- 10