0

How do I write a script to permanently change a static html file after making an ajax call to the node.js server? Any examples would be greatly appreciated :)

2 Answers 2

2

I agree with NikxDa that this is probably not the best solution for you, but this code should do the trick.

/write.js

    var http = require('http');
    var fs = require('fs');
    var url = require('url');

    //Lets define a port we want to listen to
    const PORT=8080;

    function handleRequest(request, response){
      var path = url.parse(request.url).pathname;
      if(path=="/write"){
        fs.appendFile('message.html', 'Node.js!', function (err) {
          if (err) throw err;
          console.log('It\'s saved!');
        });
      } else {
        fs.readFile('index.html',function (err, data){
          response.writeHead(200, {'Content-Type': 'text/html','Content-Length':data.length});
          response.write(data);
          response.end();
      });
      }
    }

    // Create a server.
    var server = http.createServer(handleRequest);

    server.listen(PORT, function(){
        console.log("Server listening on: http://localhost:%s", PORT);
    });

/index.html

    <!DOCTYPE html>
    <head>
    <script>
    function writeIt()
    {
       xmlhttp = new XMLHttpRequest();
       xmlhttp.open("GET","http://localhost:8080/write", true);
       xmlhttp.onreadystatechange=function(){
             if (xmlhttp.readyState==4 && xmlhttp.status==200){
               string=xmlhttp.responseText;
               document.write(string + ": Saved change to message.html");
             }
       }
       xmlhttp.send();
    }
    </script>
    </head>
    <body>
      <p>Click the button to send an AJAX request to `write.js`<p>
      <br><button onclick="writeIt()">Click Me</button>
    </body>

/message.html

Node.js!
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! I actually solved the issue myself by rewriting my entire html file using the fs.writeFile(...) method but thanks anyways! This also works and would've saved me hours of time had I seen you use the fs module earlier :D
0

Editing the file directly via node would probably be really bad, I do not even know if it is at all possible. I think the better solution is for your Node Server to make the data you want to change accessible and then use jQuery or Angular to update the HTML-File when it is actually loaded.
Another approach would be to use a templating engine like https://github.com/tj/ejs, and then serve the file via Node directly, so you can change the data in the Node-Application itself every time.

1 Comment

No. Or better, depends on how you look at it. The latter option does not use HTML files, it instead uses EJS files which you have to serve via node.js. So if you update the value and save it somewhere, e.g. a database, the final HTML-Output to the browser will permanently be changed. What exactly are you trying to achieve?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.