Node.js progress indicator feedback

后端 未结 2 521
花落未央
花落未央 2021-01-03 07:17

I have a question about the progress of sending with Node.js \'request\'. I have a Node.js application that acts as a proxy, actually forwarding a body to another server. I

相关标签:
2条回答
  • 2021-01-03 07:33

    Solution for first part was in r.req.connectin.bytesWritten

    var r = request(requestOptions, function (error, response, body) {
        clearInterval(q);
        ...
    });
    
    var q = setInterval(function () {
        console.log("Uploaded: " + r.req.connection.bytesWritten);
    }, 250);
    
    0 讨论(0)
  • 2021-01-03 07:52

    Your question contains two parts. One is for getting the progress from request, which can be found here: Upload Progress — Request

    The other part would be notifying the browser of the progress. The HTTP protocol does not let you send multiple responses, so using res.send(progress) is not possible.

    However, you can keep sending partial responses until it finishes. Writing something to res without closing it is as simple as res.write("string"), but accessing the response can be harder: Accessing partial response using AJAX or WebSockets?

    You also need a way to wrap the body (and also errors) from the backing server so that it can fit as the final partial response.

    Another solution would be opening another WebSocket request to track the uploading/downloading process. socket.io is a good library for node for this purpose.

    0 讨论(0)
提交回复
热议问题