我正在通过POST路径接收JSON格式的事件,我想将这些事件保存在一个类似于‘Example.json’的文件中,并能够查询它.
我try 使用WriteFileSync,但它会重写整个文件.使用标志{FLAG:‘A+’}我可以保存多条记录,但当我try 请求‘Example.json’时,我收到错误‘Underful Token{in JSON’.
当文件只有一条记录时工作正常,但在第二条记录之后出现错误.
Code:个
const filePath = './example.json';
const fs = require('fs');
const file = require('./example.json');
app.post('/events', (request, response) => {
response.send(request.body);
const contentString = JSON.stringify(request.body);
return fs.writeFileSync(filepath, contentString, {flag: 'a+'});
});
example.json that works:个
{"type":"call.new","call_id":"71252742562.40019","code":"h9e8j7c0tl0j5eexi07sy6znfd1ponj4","direction":"inbound","our_number":"1130900336","their_number":"11999990000","their_number_type":"mobile","timestamp":"2020-04-01T00:00:00Z"}
example.json (with two records) that stop working:个
{"type":"call.new","call_id":"71252742562.40019","code":"h9e8j7c0tl0j5eexi07sy6znfd1ponj4","direction":"inbound","our_number":"1130900336","their_number":"11999990000","their_number_type":"mobile","timestamp":"2020-04-01T00:00:00Z"}{"type":"call.ongoing","call_id":"71252731962.40019","code":"h9e8j7c0tl0j5eexi07sy6znfd1ponj4","direction":"inbound","our_number":"1130900336","their_number":"11999990000","their_number_type":"mobile","timestamp":"2020-04-01T00:00:00Z"}
如何以可读的形式编写此JSON?这不会出现上述错误,并且可以执行要求.
有没有人能帮帮我?