如何在express.js中的post请求中返回数据?

时间:2018-04-12 14:34:44

标签: node.js reactjs express

在我的反应前端内部,我有一个名为handleDrop的处理程序,代码如下:

         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
          }

在我的快速后端,我有一个这个获取请求的帖子处理程序,我迭代上传的数据并找出最常用的单词。代码如下:

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')
      }
    });
});

我的问题是:有没有办法在此发布请求中返回包含最常用单词的json格式数据,以便我可以在react组件中执行set状态并更新网页?谢谢!

0 个答案:

没有答案
相关问题