how to save file after writing nodejs code example
Example 1: data not write in file node js
const fs = require('fs');
fs.readFile('file.txt', 'utf-8', (err, data) => {
if(err) {
throw err;
}
console.log(data);
});
Example 2: how to write to a file with javascript without nodejs
function convertToJSON() {
var firstname = document.getElementById('firstname').value;
var lastname = document.getElementById('lastname').value;
var email = document.getElementById('email').value;
var jsonObject = {
"FirstName": firstname,
"LastName": lastname,
"email": email
}
document.getElementById('output').value = JSON.stringify(jsonObject)
}
function saveToFile() {
convertToJSON();
var jsonObjectAsString = document.getElementById('output').value;
var blob = new Blob([jsonObjectAsString], {
type: 'octet/stream'
});
console.log(blob);
var anchor = document.createElement('a')
anchor.download = "user.json";
anchor.href = window.URL.createObjectURL(blob);
anchor.innerHTML = "download"
anchor.click();
console.log(anchor);
document.getElementById('output').append(anchor)
}