0

I am currently buildings proxy using nodejs, which use following syntax for sending and receiving https request and response. However in my project, the response is a liitle bit larger, so typically, req.on('data', callback) will be called 5~7 times before req.on('end', callback) being called.

Here is the simplified code structure:

var http = require("https");
var options = {
    hostname: '<WEB SERVICE>',
    port: 80,
    path: '<WEB PATH>',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    }
};
var response = "";
var req = http.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (body) {
        console.log("data");
        response += body;
    });

    res.on('end', function () { 
        console.log("end");
        response = "";
    });
});
req.on('error', function(e) {
     console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('<SOMETHING>');
req.end();

Ideally, when multiple request comes in, the logging sequence shall be:

data, data, data, data, data, end, data, data, data, data, end

i.e. once one request is done, end will be called once.

However, after doing several tests, when response is big. The sequence becomes:

<response 1 comes>data, data, data ..... data <response 2 comes> data, data,       data, ..., data, end

i.e. the end for request 1 is missing.

In short, we need to make sure the callback of 'end' is called exactly once immediate after doing several call back of req.on('data', callback).

I believe there must be some common method for solving this issues (seems a classic bugs in node) and would be appreciated if anyone can indicate how to solve this property.

Thanks for the help!

Chen Chen
  • 11
  • 1
  • 5
  • 1
    The code you show only shows one request being made, but the log you show has multiple requests. Please show the context in which multiple requests are being made. If you want to sequence things, then that is where the code needs to be fixed, not in what you show. For example, if this is inside a `for` loop or any type of loop, then requests are not blocking so you will have multiple requests in-flight at the same time. Requests like this are asynchronous. They don't wait to complete before running the rest of your code. – jfriend00 Apr 04 '17 at 16:28
  • thanks! autually they are in a loop indeed, the above code is only the part in a loop. However, the problem I met is that sometimes the response req.on('end') didn't emit, causing the buffer not being cleared (response doesn't reassembed correctly). Do you have any ideas regarding this problem? Thanks! – Chen Chen Apr 05 '17 at 16:43
  • If you want help with multiple requests, you will have to disclose the code that makes multiple requests. Don't know how we can help otherwise. You will find that stackoverflow works a lot more effectively for you and more people are willing to help you if you don't wait until the next day to respond to people trying to help you. This place does not work the same as internet forums where you dump a question and come back the next day to see what happened. This place works much better when it's more interactive. – jfriend00 Apr 05 '17 at 17:06

2 Answers2

0

From the code that you included it is impossible to make two requests. It makes one request with this:

var req = http.request(options, function(res) { ... });

Then binds the error handler here:

req.on('error', function(e) { ... });

And then immediately before even waiting for any response to the request, before even a connection is being made, it calls .write() and .end() methods on the request object:

req.write('<SOMETHING>');
req.end();

Nothing here can possibly cause two requests being made at the same time. But even if the first (and only) request hasn't started yet you already call .write() and .end() methods so maybe there's your problem.

In addition to that you should expect having one request being started before the other one finishes if you are going to do few requests in parallel as you're saying you'd like to.

rsp
  • 107,747
  • 29
  • 201
  • 177
  • thanks for your hint @rsp, actually the above code is in a separate helper function, which will be called by main proxy. It will call this function frequently for sending packets to another service. So I think it will be more precise if I say call this function for multiple times and send many request frequently. However, it looks like the some response doesn't emit req.on('end') callback. **Do you mean that if I call this routine multiple times to send multiple request, I need to make sure the the code snippet is called AFTER the previous request is initiated??** Cheers! – Chen Chen Apr 04 '17 at 17:41
0

i have the same issue before, i fixed like this:

res.on('finish', function () { 
    console.log("res finished");
});

Look here nodejs.org event-finish

Jaber Alshami
  • 336
  • 3
  • 14