【发布时间】:2017-03-11 14:50:59
【问题描述】:
我想在 Node Js 中已创建的文件中写入文本。这是我所做的
上传.html
<form enctype ="multipart/form-data" action ="/file" method ="post">
MAC Address:<br>
<input type="text" name="macadd" id="macadd"><br>
Percentage:<br>
<input type="text" name="percent" id="percent"><br>
<input type="submit" value='Submit' id="upload">
<br>
</form>
App.js
app.route('/file').post(function (req,res,next) {
var macadd =req.body.macadd;
//var percent =req.body.percent;
var path ="C:\Proj\doc\data.txt";
var data ="hello";
fs.writeFile(path,macadd , function(error) {
if (error) {
console.error("write error: " + error.message);
} else {
console.log("Successful Write to " + path);
}
});
});
我想将 'macadd' 的值写入文件 data.txt。它主要是一个整数值,但是当我单击提交时,在文件中它显示为“未定义”,即使它在控制台上显示为成功写入。有什么解决办法吗?
【问题讨论】:
标签: javascript node.js filesystems