0

I'm currently running Node.js by Browserify for my website. It reads the JSON file and I get the message through MQTT. But the problem is that it seems like writefile does not work. (Running this as node test.js in the terminal works by the way).

What is wrong with my code? Moreover, Is this the best way to store any user data?

Thank you so much in advance.

Here's some part of my code

var fs = require("fs");
var path = require("path");
let newFile = fs.readFileSync('/home/capstone/www/html/javascript/test.json');

function testT() { //THIS WORKS FINE

    let student0 = JSON.parse(newFile); 
    var myJSON = JSON.stringify(student0);
    client.publish("send2IR", myJSON);
    response.end();
};

function write2JSON() { //PROBLEM OF THIS CODE
        const content = 'Some content!'
        fs.writeFileSync('/home/capstone/www/html/javascript/test.json', content)

};

document.getElementById("blink").addEventListener("click", publish);
document.getElementById("write").addEventListener("click", write2JSON);

JunCho
  • 13
  • 3

1 Answers1

0

You cann't write directly for security reasons. For other hand you can use a server as API to do the filye system tasks and in the client only trigger the events.

This post is very related with your problem: Is it possible to write data to file using only JavaScript?

roag92
  • 146
  • 1
  • 6