Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js-response.write(…)不工作_Javascript_Node.js - Fatal编程技术网

Javascript Node.js-response.write(…)不工作

Javascript Node.js-response.write(…)不工作,javascript,node.js,Javascript,Node.js,我有以下node.js代码(拦截代理): var http=require('http'); var eamorr=require('./eamorr_addon/out/Release/eamorr_addon'); createServer(函数(请求、响应){ var proxy=http.createClient(80,request.headers['host']) var proxy_request=proxy.request(request.method、request.url、r

我有以下node.js代码(拦截代理):

var http=require('http');
var eamorr=require('./eamorr_addon/out/Release/eamorr_addon');
createServer(函数(请求、响应){
var proxy=http.createClient(80,request.headers['host'])
var proxy_request=proxy.request(request.method、request.url、request.headers);
proxy\u request.addListener('response',函数(proxy\u response){
proxy_response.addListener('data',函数(区块){
var arr=eamorr.analysis(块);

对于(var i=0;i您的分析函数是同步的还是异步的?如果它做了任何异步的事情,看起来您在那里留下了一个缺口,在分析调用后写入最后一个数据块之前,响应可能会结束。看起来它可能是完全同步的,但即使需要一段时间来处理,它仍然可能是异步的或者在on数据侦听器和on end侦听器之间可能存在竞争条件

如果事实证明确实如此,则a)在处理和中继代理数据时添加某种简单的互斥锁,以防止响应过早结束;或b)只需移动响应。将(“2”)写入代理响应端侦听器,就在响应之前。end()行,所以它总是发生在同一个回调中,不能被抢占


如果结构更改所施加的限制不妨碍您的整体应用程序设计,那么b显然会容易得多。假设我正确地猜测了实际问题是什么,我可能会完全偏离基准,但这看起来确实是可能的。

我遇到了与此类似的问题,我发现这是由于“content length”标头。客户端希望响应正文具有一定的长度,如果超过该长度,则会切断多余的长度。因此,如果向响应正文添加内容,但不调整发送给客户端的“content length”标头,则会得到所看到的结果


在我的例子中,我刚刚删除了“content-length”标题,它解决了这个问题,尽管我还没有完全意识到这样做的含义。

写数组元素以响应的for循环如何,这是否适用于大型数组?嘿,刚从午餐回来。它适用于小型数组(我只返回小数组)。你不应该使用“二进制”编码。但不确定这是否是问题的根源。不确定,但值得一试。现在开会。我将在几分钟后重试并报告。非常感谢,非常感谢你的详细答复。我尝试了你的建议b)--写了几篇“2”“response.end()之前的字符串。它们出现在Wireshark中,但另一端的Node.js程序没有接收到。我现在要尝试互斥建议。我正在努力弄清这一点。我的函数(分析)是同步的,但它位于异步块内,因此程序中不会出现任何明显的挂起。
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);