Javascript 如何通知HTTP客户端长任务已完成

Javascript 如何通知HTTP客户端长任务已完成,javascript,node.js,http,express,Javascript,Node.js,Http,Express,我有一个Node.js系统,它将大量对象上传到MongoDB,并在dropbox中为每个对象创建文件夹。每个对象大约需要0.5秒。因此,在我有许多对象的情况下,这可能需要大约一分钟的时间。我当前要做的是使用202响应代码通知客户端对象数组已被接受。但是,我如何在一分钟后通知客户完成 app.post('/BulkAdd', function (req, res) { issues = [] console.log(req.body) res.status(202).sen

我有一个Node.js系统,它将大量对象上传到MongoDB,并在dropbox中为每个对象创建文件夹。每个对象大约需要0.5秒。因此,在我有许多对象的情况下,这可能需要大约一分钟的时间。我当前要做的是使用
202
响应代码通知客户端对象数组已被接受。但是,我如何在一分钟后通知客户完成

app.post('/BulkAdd', function (req, res) {
    issues = []
    console.log(req.body)
    res.status(202).send({response:"Processing"});
    api_functions.bulkAdd(req.body).then( (failed, issues, success) => {
        console.log('done')
    })

});


bulkAdd: async function (req, callback) {
  let failed = []
  let issues = []
  let success = []
  i = 1

  await req.reduce((promise, audit) => {
    // return promise.then(_ => dropbox_functions.createFolder(audit.scanner_ui)
    let globalData;
  return promise.then(_ => this.add(audit)
      .then((data)=> {globalData = data; return dropbox_functions.createFolder(data.ui, data)}, (error)=> {failed.push({audit: audit, error: 'There was an error adding this case to the database'}); console.log(error)})
        .then((data)=>{console.log(data, globalData);return dropbox_functions.checkScannerFolderExists(audit.scanner_ui)},(error)=>{issues.push({audit: globalData, error: 'There was an error creating the case folder in dropbox'})})
         .then((data)=>{return dropbox_functions.moveFolder(audit.scanner_ui, globalData.ui)},(error)=>{issues.push({audit: globalData, error: 'No data folder was found so an empty one was created'}); return dropbox_functions.createDataFolder(globalData.ui)})
          .then(()=>success.push({audit:globalData}), issues.push({audit: globalData, error: 'Scanner folder found but items not moved'}))
    );
  }, Promise.resolve()).catch(error => {console.log(error)});
  return(failed, issues, success)

},

让客户机请求等待的问题是,它会在一定时间后超时,或者有时会显示错误,没有收到响应

你能做的就是

 - Make client request to server to initiate the task, and return 200OK and keep doing your task on server. 
 - Now write a file on server after insertion of every object as status. 
 - Read the file from client every 5-10 sec to check if server has completed creating objects or not. 
 - Mean while your task is not completed on server, show status with completion percentage or some animation.

或者简单地实现
WebHook
WebSockets
来保持通信

因为您没有包含任何代码,所以我建议您使用
setTimeout(()=>{notifyUser();},60000)
如果您使用的是
node
查看类似
node-Webhooks
的内容,HTTP响应是无状态的,请注意:一旦返回页面,就不再有交互。有两种选择,1:使用长的极化请求(但这可能很难正确实现)。2.使用WebSocket传达进度。事实上,从一开始就使用WebSocket是node.js与HTTP客户端通信的一种非常好的方式。