0

I'm trying to build an application that will create html files for me and i have decided that node.js might be the best solution for me.

Node is installed and the server is set up but i'm having some issue to call the functions.

Any ideas on how i can make this work?

const http = require('http');
const fs = require('fs');
const hostname = "127.0.0.1";
const port = "8000";


fs.readFile('index.html', (err, html) => {
    if(err){
        throw err;
    }
    const server = http.createServer((req, res) => {
        res.statusCode = 200;
        res.setHeader('Content-type', 'text/html');
        res.write(html);
        res.end();
    });

    server.listen(port, hostname, () => {
        console.log('Server started on ' + hostname + ':' + port)
    });
});


document.getElementById("one").addEventListener("click", function writeFile() {
    var fs = require('fs');
    fs.appendFile('new.html', a + b + c, function (err) {
      if (err) throw err;
      console.log('Saved!');
    });
    });
<head>
    <title>Editor</title>
    <script src="app.js"></script>
</head>
<body>
    <center>
    <button id="one">Update</button>
    <h1 id="two"></h1>
    </center>
</body>
jberling
  • 3
  • 1
  • 3
  • browser does not have access to your file system, what you could do instead is prompt the download of the file – Krzysztof Krzeszewski Dec 04 '19 at 13:28
  • 2
    You've got some fairly broad misunderstandings about node in here. Node is a server-side system; you'd build APIs in it, but it doesn't remove the need to also have client-side JS to call those APIs. You've got it all munged up in a single file but this is never going to work the way you have it structured. – Joe Dec 04 '19 at 13:29

1 Answers1

-1

Browser cant handle files, due to security reason. You have to create backend service and call service either using REST/TCP and modify in the backend.

File URL "Not allowed to load local resource" in the Internet Browser

xdeepakv
  • 7,835
  • 2
  • 22
  • 32