节点appendFile没有将数据块附加到文件系统上的文件

时间:2014-06-07 09:10:51

标签: node.js

我有一个程序试图使用curl从请求中获取值并将它们存储在一个文件中并返回存储的内容。在文件中存储或附加内容的决定基于查询参数appendFlag 现在当我运行这个程序时,我在控制台中得到的是" true "和" 追加"这表明它确实读取了标志转到if部分,但不知何故,appendFile函数不起作用。

var http = require('http');
var url = require('url');
var querystring = require('querystring');
var fs = require('fs');
http.createServer(function(request,response){
    var str = request.url.split('?')[1];
    var query = querystring.parse(str);
    var writeStream = fs.createWriteStream(query['fileName']);
    console.log("query - ");
    console.log(query["appendFlag"]);
    request.on('data',function(chunk){
        if(query["appendFlag"]=="true"){
            console.log("appending");
            fs.appendFile(query['fileName'],chunk.toString(),function(err){
                if(err) throw err;
            });
        }else{
            var bufferGood = writeStream.write(chunk);
            if(!bufferGood) request.pause();
        }
    });
    request.on('end',function(){
        response.writeHead(200);
        response.write("\n Content with this url is - \n");
        var readStream = fs.createReadStream(query['fileName'],{bufferSize:64*1024});
        readStream.on('data',function(chunk){
            response.write(chunk.toString());
        }); 
        readStream.on('end',function(){
            response.write("\n");
            response.end();
        });
    });
    writeStream.on('drain',function(){
        request.resume();
    });
}).listen(8080);

然后在读完SO(How to create appending writeStream in Node.js)的答案后,我试过了 -

// Program to extract url from the request and writing in that particular file

var http = require('http');
var url = require('url');
var querystring = require('querystring');
var fs = require('fs');
http.createServer(function(request,response){
    var str = request.url.split('?')[1];
    var query = querystring.parse(str);
    var writeStream = fs.createWriteStream(query['fileName']);
    options = { 
        'flags': 'a' ,
        'encoding' : null,
        'mode' : 0666
    }
    var appendStream = fs.createWriteStream(query['fileName'],[options]);
    console.log("query - ");
    console.log(query["appendFlag"]);
    request.on('data',function(chunk){
        if(query["appendFlag"]=="true"){
            console.log("appending");
            var bufferGood = appendStream.write(chunk.toString());
            if(!bufferGood) request.pause();
        }else{
            var bufferGood = writeStream.write(chunk.toString());
            if(!bufferGood) request.pause();
        }
    });
    request.on('end',function(){
        response.writeHead(200);
        response.write("\n Content with this url is - \n");
        var readStream = fs.createReadStream(query['fileName'],{bufferSize:64*1024});
        readStream.on('data',function(chunk){
            response.write(chunk.toString());
        }); 
        readStream.on('end',function(){
            response.write("\n");
            response.end();
        });
    });
    writeStream.on('drain',function(){
        request.resume();
    });
}).listen(8080);

将标志更改为' a '它也没有附加数据?

1 个答案:

答案 0 :(得分:3)

您可以使用您的第一个变体。但在appendFile()之前,您已为同一writeStream打开query["filename"]。流已经打开。

var writeStream = fs.createWriteStream(query['fileName']);
options = { 
    'flags': 'a' ,
    'encoding' : null,
    'mode' : 0666
}
var appendStream = fs.createWriteStream(query['fileName'],[options]);

做以下事情可能会更好:

var options = {
  flags: query.appendFile ? 'w' : 'a'
  ...

下一篇:为什么[options]?你应该删除括号。

下一步:我们无法保证filename中有querystring个参数。请处理这种情况。