0

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

  • why would you need `mode: 'cors'` for a relative URL, i.e. same domain? Also, concatenating JSON contents will result in invalid JSON ... you'll need to read each JSON file on the server, parse it, add it to an array, and stringify the array before you send it in the response – Jaromanda X Sep 29 '20 at 04:09
  • I think I just forgot to delete it – Ефим Маргин Sep 29 '20 at 04:10
  • by the way `.then(console.log(this.firstWindow))` - that will log to the console before the request happens, because you're not passing a function to that .then – Jaromanda X Sep 29 '20 at 04:13
  • so, you'll need to read each json file ... see https://stackoverflow.com/questions/10011011/using-node-js-how-do-i-read-a-json-file-into-server-memory and then add each result to an array, then JSON.stringify that array and send that in the response – Jaromanda X Sep 29 '20 at 04:14
  • can I ask you one more question, not exactly on this topic? I changed the code in the question a bit – Ефим Маргин Sep 29 '20 at 04:30

0 Answers0