node and socket.io multiple API calls hang after a while

后端 未结 2 1027
生来不讨喜
生来不讨喜 2021-01-28 12:19

I have a node express/ socket application where my express server makes several API calls with node-rest-client looping through elements in var jobs and when each finishes, it s

2条回答
  •  一个人的身影
    2021-01-28 13:20

    It turned out to be the client.get() request causing the error. Here is my code to fix this. It still errors, but at least the error is handled and wont cause the node server to crash. If there is a more eloquent way of handling this, please let me know!

    setInterval(function(){
      var jobs = ['J1', 'J2', 'J3', 'J4'];
      var full_data = {};
      for(var i = 0; i < jobs.length; i++){
        client.get("MY URL", function (data, response) {
            io.sockets.emit('progressbar', data);
          }).on('error', function (err) {
            console.log('something went wrong on the request', err.request.options);
        });
      }
      console.log(full_data);
    
    }, 5000)
    

提交回复
热议问题