1

Im using node with express and I am calling within my node server a http get request - now when the complete data arrived from the http-request I want to call res.send with the received data. My code looks as follows:

app.get('/', function (req, res) {

const x = https.get(options, (req) => {
    console.log('statusCode:', req.statusCode);
    console.log('headers:', req.headers);

    req.on('data', (locations) => {
        buffer = new Buffer(locations);
        console.log(buffer);
        res.send(buffer);      <== should sent complete result
    });

}).on('error', (err) => console.log('error:', err));

x.end();
});

app.listen(port, function () {
    console.log('Example app listening on port 3000!');
});

The Problem With my Code: I get everytime a different size of data - basically - everytime a incomplete set of data.

  • Best to just pipe the request to your res object. Then, data will be sent automatically as it arrives by the incoming stream. `req.pipe(res)`. Also, I'd suggest not using `req` for both arguments that are in scope because of the potential for confusion. – jfriend00 Jun 16 '18 at 14:39
  • You can use `.pipe()` with `http.get()` or with the `request()` library as shown in the question yours has been marked a dup of. – jfriend00 Jun 16 '18 at 14:50
  • Also see: [Piping remote file in express](https://stackoverflow.com/questions/18432779/piping-remote-file-in-expressjs?noredirect=1&lq=1) – jfriend00 Jun 16 '18 at 14:51
  • Why cant I mark your answer as the solution? +1 for your advice. It worked. –  Jun 16 '18 at 14:53
  • Your question has been marked a duplicate. There is no new answer to accept. Glad it worked for you. – jfriend00 Jun 16 '18 at 14:55
  • Another relevant dup: [Pipe response with http.get()](https://stackoverflow.com/questions/37899263/pipe-function-in-node-and-http-get) – jfriend00 Jun 16 '18 at 14:55

0 Answers0