I have a FETCH request to the server that sends me a json file. But I need to get two different files. Please tell me how I can implement this ?
================vue code
mounted(){
fetch('/', {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
})
.then(response => response.json())
.then(json => this.firstWindow = json)
.then(json => console.log(json))
.then(console.log(this.firstWindow))
},
===========node.js code
router.post('/',(req, res) =>{ // request for sending file
// console.log("POST");
res.sendFile(path.resolve('./data/firstScreen.json'));
res.sendFile(path.resolve('./data/secondScreen.json'));
})
here I am trying to generate an array of Json files to send to the server. But after I write it inside fs it remains empty
=================== work with fs
let dataOfScreens = [];
let Mode = require('stat-mode');
let temp;
fs.readdir(path, function(err, items) {
for (let i=0; i<items.length; i++) {
if(items[i].indexOf("System Volume Information")!= -1 || items[i].indexOf("hiberfil")!= -1 || items[i].indexOf("pagefile")!= -1 || items[i].indexOf("MSOCache")!= -1 || items[i].indexOf("swapfile")!= -1 || items[i].indexOf("Recovery")!= -1){
continue;
}
fs.stat(path + items[i], function(err, stats){
if(err) throw err;
let mode = new Mode(stats);
if(mode.isDirectory()){
temp = "<папка>";
} else{
temp = stats.size.toString() + " байт";
}
firstJson.table.push({id:i, icon:i, fileName:items[i], sizeOrType: temp , dateOfChange: stats.mtime});
dataOfScreens.push(firstJson); // HERE !
fs.writeFile('data/firstScreen.json', JSON.stringify(firstJson), (err)=>{
if(err) console.log("error");
});
secondJson.table.push({id:i, icon:i, fileName:items[i], sizeOrType: temp , dateOfChange: stats.mtime});
dataOfScreens.push(secondJson);// HERE !
fs.writeFile('data/secondScreen.json', JSON.stringify(secondJson), (err)=>{
if(err) console.log("error");
});
});
}
});
dataOfScreens.push(JSON.parse(fs.readFileSync('data/firstScreen.json', 'utf8')));
dataOfScreens.push(JSON.parse(fs.readFileSync('data/secondScreen.json', 'utf8'))); // my solution
console.log(dataOfScreens);
I'm using a very stupid solution right now