Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html appendFile正在替换内容_Html_Node.js_Append - Fatal编程技术网

Html appendFile正在替换内容

Html appendFile正在替换内容,html,node.js,append,Html,Node.js,Append,我刚从node.js开始,遇到了appendFile,它将内容附加到文件的末尾,但当我执行它时,我的代码id将内容全部替换为数据 这是代码 var http = require('http'); var file = require('fs'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); var fd = file.o

我刚从node.js开始,遇到了appendFile,它将内容附加到文件的末尾,但当我执行它时,我的代码id将内容全部替换为数据

这是代码

var http = require('http');
    var file = require('fs');
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/html'});
      var fd = file.open('default.html','w',function(err){
        if(err)throw err;
    });
    file.appendFile('default.html',"<p>Hi,this to inform you that i really don't care</p>",function(err){});
      return res.end();
    }).listen(8080);
defalult文件是

 <!doctype>
<html lang="en">
<head>
    <title>File</title>
</head>
<body>
    <h1>
        Hi, EveryOne
    </h1>
</html>
我得到的输出是

<p>Hi,this to inform you that i really don't care</p>

有人能告诉我为什么会发生这种情况,或者我做错了什么吗???

因为您已在“w”即写入模式下打开文件,然后对其执行操作。在附加到文件之前,确实不需要打开该文件

var fd = file.open('default.html','w',function(err){
        if(err)throw err;
    });
我看不出,你用它做了什么有用的事情,所以不需要这个代码。如果确实需要,请在“附加”模式下打开文件,将“w”替换为“a”,并记住关闭文件

但是,正如我所说的,您可以简单地使用该方法进行附加,而不是打开文件