const fs = require('fs');
export default {
...
changeJSON(){
fs.readFile('todo.json','utf-8',(err,data) => {
let obj = this.todos
fs.writeFile('todo.json', JSON.stringify(obj), (err) => {
if (err)
console.log(err);
})
})
},
...
export default
, то это написано все в файле с раcширением vue, я к тому что такая записьmodule.exports
возможно не правильная, по крайней мере я не встречал const express = require('express');
const fs = require("fs");
const app = express();
app.use('/', express.static('dist'));
app.use(express.json());
app.post('/todo.json', (req, res) => {
let objJSON = req;
fs.writeFile('/todo.json', objJSON, (err) => {
if(err){
console.log(err)
}
})
})
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listen on port ${port}...`));
postJson(url, data){
return fetch(url, {
method: 'POST',
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
})
.then(result => result.json())
},
this.postJson('todo.json', this.todos).
then(data => {
data = this.todos
})
app.use(express.json());
положит распаршенный json в req.body, а для записи в файл его нужно будет опять сериализовать, то есть проблема в том что вы пишите в файл не json, да еще и бесполезной работой сервер нагружаете.const express = require('express');
const fs = require("fs");
const app = express();
app.use('/', express.static('dist'));
app.post('/todo.json', (req, res) => {
req.pipe(fs.createWriteStream('./todo.json'))
.once('finish', () => {
res.writeHead(201);
res.end();
});
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listen on port ${port}...`));