使用 javascript 和 node.js 如何通过 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!");
}
});