Using javascript and node.js how to post the content to the text file through ajax.
<html>
<body>
<input type="button" value="Click Me" onclick="postContent()">
<script>
function postContent(){
var req=new XMLHttpRequest();
req.open('post','addContent.js',true);//in this sample.txt file i want to insert some content
req.send();
}
</script>
</body>
</html>
addContent.js
var fs = require('fs');
fs.writeFile("/tmp/sample.txt", "Hey there!", function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
You can have a server-side script that writes the content to a text file and call that server-side script using Ajax.
req.open('POST', 'FilePoster', true);
req.send(dataToWrite);
Edited for new question:
req.open('post','addContent.js',true);
addContent.js should be an HTTP server running on a host and port.
If addContent.js is a node HTTP server, running on localhost and port 8080, the open call should be
req.open('POST', 'localhost:8080', true);