node.js将数据附加到文件的代码

时间:2022-02-23 14:07:28

How can I append data to a file using node.js

如何使用node.js将数据附加到文件

I already have a file named myfile.json with data. I want to check if the file name exists and then append some data to that file.

我已经有一个名为myfile.json的文件和数据。我想检查文件名是否存在,然后将一些数据附加到该文件。

I'm using following code

我正在使用以下代码

var writeTempFile = function (reportPath, data, callback) {   
    fs.writeFile(reportPath, data, function (err) {
        //if (err) //say(err);
        callback(err);
    });
}
writeTempFile(reportDir + '_' + query.jobid + ".json", data, function (err) {
    context.sendResponse(data, 200, {
        'Content-Type': 'text/html'
    });

3 个解决方案

#1


1  

You can use jsonfile

你可以使用jsonfile

var jf = require('jsonfile');
var yourdata;
var file = '/tmp/data.json';
jf.readFile(file, function(err, obj) {
    if(!err) {
        var finalData = merge(obj, yourdata);

        jf.writeFile(file, finalData, function(err) {
            console.log(err);
        });
    }
});

You need to implement your merging logic in merge(object1, object2)

您需要在merge(object1,object2)中实现合并逻辑

https://npmjs.org/package/jsonfile

#2


0  

Check out the following code.

查看以下代码。

function addToFile(reportPath, data, callback){
    fs.appendFile(reportPath, data, function (err) {
        callback(err);
    });
}

#3


0  

Node offers fs module to work with file system. To use this module do

Node提供fs模块来处理文件系统。要使用这个模块吗

var fs = require('fs')

To append some data to file you can do:

要将一些数据附加到文件,您可以执行以下操作:

fs.appendFile('message.txt', 'data to append', function (err) {
  if (err) throw err;
 console.log('The "data to append" was appended to file!');
});

Node offers you both synchronous and asynchronous method to append data to file, For more information please refer to this documentation

Node为您提供了将数据附加到文件的同步和异步方法,有关详细信息,请参阅此文档

#1


1  

You can use jsonfile

你可以使用jsonfile

var jf = require('jsonfile');
var yourdata;
var file = '/tmp/data.json';
jf.readFile(file, function(err, obj) {
    if(!err) {
        var finalData = merge(obj, yourdata);

        jf.writeFile(file, finalData, function(err) {
            console.log(err);
        });
    }
});

You need to implement your merging logic in merge(object1, object2)

您需要在merge(object1,object2)中实现合并逻辑

https://npmjs.org/package/jsonfile

#2


0  

Check out the following code.

查看以下代码。

function addToFile(reportPath, data, callback){
    fs.appendFile(reportPath, data, function (err) {
        callback(err);
    });
}

#3


0  

Node offers fs module to work with file system. To use this module do

Node提供fs模块来处理文件系统。要使用这个模块吗

var fs = require('fs')

To append some data to file you can do:

要将一些数据附加到文件,您可以执行以下操作:

fs.appendFile('message.txt', 'data to append', function (err) {
  if (err) throw err;
 console.log('The "data to append" was appended to file!');
});

Node offers you both synchronous and asynchronous method to append data to file, For more information please refer to this documentation

Node为您提供了将数据附加到文件的同步和异步方法,有关详细信息,请参阅此文档