Inside my react frontend, I have a handler called handleDrop, the code looks like:
handleDrop(files) {
var data = new FormData();
alert((files[0]) instanceof File);
files.forEach((file, index) => {
data.append('file' + index, file);
});
fetch('/file_upload', {
method: 'POST',
body: data
}
At my express backend, I have a post handler for this fetch request where I iterate over the uploaded data and figured out the most frequent word. The code looks like:
app.post('/file_upload', function(req , res){
var dictCount ={};
var storage = multer.diskStorage({
destination: mypath
});
var upload = multer({
storage: storage
}).any();
upload(req, res, async err => {
try {
if (err) {
console.log(err);
return res.end('Error');
} else {
await bluebird.each(req.files, async item => {
const data = fs.readFileSync(item.path);
// figure out the word count here
var topFeatures = {};
Object.keys(dictCount).forEach(function(key) {
console.log(dictCount[key].count);
if(dictCount[key].count>6){
topFeatures[key] = {word:key, count:dictCount[key].count};
}
});
//console.log(topFeatures);
// how to send top features back to the frontend?
//res.send({"name":"lol"});
res.end('File uploaded');
}
} catch (e) {
res.end('Error')
}
});
});
My question is: Is there a way to return a json format data containing the most frequent word in this post request so I can do a set state in my react component and update the webpage? Thanks!