Q
Q
q2mber22015-08-11 04:17:33
JavaScript
q2mber2, 2015-08-11 04:17:33

How to understand that response.write is finished?

var https = require('https'),
  _ = require('underscore');

function get(response, command) {
  var options = {
    hostname: 'poloniex.com',
    path: '/public?command=' + command,
    method: 'GET'
  };

  var req = https.request(options, function (res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
      response.write(chunk);
    });
  });
  req.end();
  
  response.setTimeout(3000, function () {
    response.end();
  })

}

exports.get = get;

The answer is big and Node.js writes it in chunks. How to understand that the recording is over and you can complete the response? While I complete it on a timeout.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Shemsedinov, 2015-08-11
@q2mber2

// после res.on('data') вставляем еще обработчик:
res.on('end', function() {
  response.end();
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question