2011-06-04 103 views
3

我正在处理一个涉及在另一个服务前使用node.js作为代理服务器的狡猾计划。node.js http.request事件流 - 我的END事件发生在哪里?

简而言之:

  1. 调度传入请求静态文件(如果存在的话)
  2. 否则,将请求调度到另一个服务

我有基础的工作,但现在试图让整个事情与Sencha Connect一起工作,这样我就可以访问所有提供的kick-ass中间件。

所有动作都发生在dispatchProxy下面

connect(
    connect.logger(), 
    connect.static(__dirname + '/public'), 
    (request, response) -> 
    dispatchProxy(request, response) 
).listen(8000) 

dispatchProxy = (request, response) -> 

    options = {host: host, port: port, method: request.method, headers: request.headers, path: request.url} 

    proxyRequest = http.request(options, (proxyResponse) -> 
    proxyResponse.on('data', (chunk) -> 
    response.write(chunk, 'binary') 
    ) 

    proxyResponse.on('end', (chunk) ->   
    response.end() 
    ) 

    response.writeHead proxyResponse.statusCode, proxyResponse.headers  
) 

    request.on('data', (chunk) -> 
    proxyRequest.write(chunk, 'binary') 
) 

    # this is never triggered for GETs 
    request.on('end', -> 
    proxyRequest.end() 
) 

    # so I have to have this here 
    proxyRequest.end() 

你会发现proxyRequest.end()最终线之上。

我发现的是,当处理GET请求时,请求的END事件永远不会被触发,因此需要调用proxyRequest.end()。按照预期,POST请求触发DATA和END事件。

那么几个问题:

  • 这是调用proxyRequest.end()安全吗?也就是说,即使在事件循环之外调用proxyResponse,proxyResponse是否仍会完成?

  • GET不会触发END事件,或者END被捕获到连接堆栈的某个地方是正常的吗?

+0

'finish'事件似乎按照建议[here](http://stackoverflow.com/a/18255507) – 2015-01-21 14:12:45

回答

4

的问题是少end事件多的data事件。如果客户端发出GET请求,则会有标头并且没有数据。这与请求者发送数据的POST不同,因此on("data")处理程序被击中。所以,(原谅我的JS例如,我不是那熟悉的CoffeeScript):

var http = require('http'); 

// You won't see the output of request.on("data") 
http.createServer(function (request, response) { 
    request.on("end", function(){ 
    console.log("here"); 
    }); 
    request.on("data", function(data) { 
    console.log("I am here"); 
    console.log(data.toString("utf8")); 
    }); 
    response.writeHead(200, {'Content-Type': 'text/plain'}); 
    response.end('Hello World\n'); 
}).listen(8124); 

console.log('Server running at http://127.0.0.1:8124/'); 

如果我对这个服务器的卷曲呼叫,数据事件从来没有被击中,因为GET要求什么更多比标题。正因为如此,你的逻辑变成:

// okay setup the request... 
// However, the callback doesn't get hit until you 
// start writing some data or ending the proxyRequest! 
proxyRequest = http.request(options, (proxyResponse) -> 
    // So this doesn't get hit yet... 
    proxyResponse.on('data', (chunk) -> 
    response.write(chunk, 'binary') 
) 

    // and this doesn't get hit yet 
    proxyResponse.on('end', (chunk) -> 
    // which is why your response.on("end") event isn't getting hit yet   
    response.end() 
) 

    response.writeHead proxyResponse.statusCode, proxyResponse.headers  
) 

// This doesn't get hit! 
request.on('data', (chunk) -> 
    proxyRequest.write(chunk, 'binary') 
) 

// So this isn't going to happen until your proxyRequest 
// callback handler gets hit, which hasn't happened because 
// unlike POST there's no data in your GET request 
request.on('end', -> 
    proxyRequest.end() 
) 

// now the proxy request call is finally made, which 
// triggers the callback function in your http request setup 
proxyRequest.end() 

所以,是你不得不手动调用proxyRequest.end()GET请求由于逻辑分支我刚才提到的。

+0

使用上面的示例脚本,request.on(“end”)回调*被触发在GET上,但数据事件不是......这让我觉得连接基础设施内部发生了一些奇怪的事情...... – 2011-06-04 06:45:23

0

我的经验是,request.on('end',)不是一致除非它是一个POST,否则将被调用。我怀疑在脚本有机会检测到该事件之前(发出http.request的人)事件结束。

+1

我正在与这个非常相同的问题摔跤。它很疯狂。 – Aerik 2014-02-28 16:53:01