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>