简体   繁体   中英

Node.js - response.write(…) not working

I have the following node.js code (an intercepting proxy):

var http = require('http');
var eamorr=require('./Eamorr_addon/out/Release/Eamorr_addon');


http.createServer(function(request,response){
  var proxy=http.createClient(80,request.headers['host'])
  var proxy_request=proxy.request(request.method,request.url,request.headers);
  proxy_request.addListener('response',function(proxy_response){
    proxy_response.addListener('data',function(chunk){
        var arr=eamorr.analyse(chunk);
        for(var i=0;i<arr.length;i++){
            //console.log(arr[i]+"\n\n");
            response.write(arr[i]);
        }
        response.write("2");   //this doesn't get written!
    });
    proxy_response.addListener('end',function(){
      response.end();
    });
    response.writeHead(proxy_response.statusCode,proxy_response.headers);
  });
  request.addListener('data',function(chunk){
    proxy_request.write(chunk,'binary');
  });
  request.addListener('end',function(){
    proxy_request.end();
  });
}).listen(10002);

As you can see, I have a custom Node.js addon with a function analyse() which returns an array of strings. I am then looping through this array and writing the result.

The problem I am having is that "2" doesn't get written!

I'm really stuck here and was wondering if anyone can help?

Now I know that (according to the Node.js docs) that "The first time response.write() is called, it will send the buffered header information and the first body to the client. The second time response.write() is called, Node assumes you're going to be streaming data, and sends that separately. That is, the response is buffered up to the first chunk of body."

I've tried sticking in a response.end() after response.write("2") , but it still won't work.

How to flush the output or turn off streaming?

Is your analyse function synchronous or async? If it does anything async, it looks like you're leaving a gap in there, where the response may be ended before the last data chunk is being written after the analyse call. It looks like it's probably totally synchronous, but even if it takes awhile to process, it still might be possible for there to be a race condition between the on data listener and the on end listener firing.

If that turns out to be truly what's going on, either a) add a simple mutex of some sort while processing and relaying the proxy data, preventing the response from being prematurely ended; or b) simply move the response.write("2") into the proxy_response end listener, right before the response.end() line, so it always happens in the same callback and can't be pre-empted.

b would obviously be much easier, if the limitations imposed by that structure change don't hamper your overall application design. And that's assuming I've correctly guesstimated what the actual problem is, I could be totally off base, but it does look possible.

I was having a similar issue as this and I found out it was due to the "content-length" header. The client is expecting the response body to be a certain length, and if it exceeds that length, it cuts off the excess. So if you're adding to the response body but not adjusting the "content-length" header that's being sent to the client, then you'll get the result you're seeing.

In my case I just deleted the "content-length" header and it fixed the problem, though I'm not fully aware yet of the implications of doing that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM