I have a node.js project and i can write to a file from the app.js file. App.js starts the server and runs the content of index.html in my public folder. The problem is that i can't write to a file from the javascript in the public folder, and i guess this is because all the javascript in there is client side. How do i call server side javascript so that i can do I/O?
Index.html - located in Public folder
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>test1</title>
</head>
<body>
<button onclick="WriteToFile()">writie to file</button> <br>
</body>
</html>
App.js
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var app = express();
// set static path
app.use(express.static(path.join(__dirname, 'public')));
app.listen(3000, function(){
console.log('Server started on Port 3000...');
})
//How do i call this function or write to a file from index.html.
function WriteToFile(){
fs = require('fs');
fs.writeFile('helloworld.txt', 'The Function was called', function (err) {
if (err)
return console.log(err);
console.log('Wrote Hello World in file helloworld.txt, just check it');
});
}