首页 > 解决方案 > 将 Excel 转换为 SQL,然后转换为 JSON,以优化在 NodeJS 中发送到客户端的数据响应

问题描述

我有一个应用程序,我们使用此方法上传包含数据的 Excel 文件:

// Upload and replace excel file for a new version of the excel file
app.post('/data', function (req, res) {
  const upload = multer({ storage }).single('document');
  fs.unlink(path2, (err) => {
    if (err) {
      console.error(err)
      return
    }
    upload(req, res, function (err) {
      if (err) {
        return res.end("error lors de upload")
      }
      res.redirect('/')
    })
  })
});

然后还有另一种方法,将excel转成JSON,将数据发送给客户端:

// Converts Excel to JSON and send to client
app.get('/json2', (req, res) => {
  node_xj({
    input: "uploads/data.xlsx",
    output: "output.json"
  }, function (err, result) {
    if (err) {
      console.error(err);
    } else {
      const interval = setInterval(() => {
        console.log('test')
      }, 3600 * 24 * 365)
      res.send(result);
      console.log("results send ok");
    }
  });
})

但是这个过程比较慢,因为每次客户端请求数据,服务器都要将excel转成JSON再发送数据,所以有时候客户端收不到数据。

我想知道是否可以通过将 Excel 数据放入 SQL 中,然后 json2 路由将 SQL 转换为 JSON 并发送到客户端来使用脚本对此进行优化。

我的 nodejs 应用程序现在部署在 Heroku 中。

你知道怎么做吗?你能给点建议吗?

提前致谢

标签: node.jsjsonexcelapiheroku

解决方案


推荐阅读