title: Call Node.js function from web page
body: <p>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.</p>
  
  <p>Node is installed and the server is set up but i'm having some issue to call the functions.</p>
  
  <p>Any ideas on how i can make this work?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const http = require('http');
  const fs = require('fs');
  const hostname = "127.0.0.1";
  const port = "8000";
  
  
  fs.readFile('index.html', (err, html) =&gt; {
      if(err){
          throw err;
      }
      const server = http.createServer((req, res) =&gt; {
          res.statusCode = 200;
          res.setHeader('Content-type', 'text/html');
          res.write(html);
          res.end();
      });
  
      server.listen(port, hostname, () =&gt; {
          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!');
      });
      });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;head&gt;
      &lt;title&gt;Editor&lt;/title&gt;
      &lt;script src="app.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;center&gt;
      &lt;button id="one"&gt;Update&lt;/button&gt;
      &lt;h1 id="two"&gt;&lt;/h1&gt;
      &lt;/center&gt;
  &lt;/body&gt;</code></pre>
  </div>
  </div>
  </p>