node.js http.request事件流 - 我的END事件在哪里? [英] node.js http.request event flow - where did my END event go?
问题描述
我正在开发一个狡猾的计划,它涉及在另一个服务之前使用node.js作为代理服务器。
简而言之:
- 将传入请求分派到静态文件
-
这是对proxyRequest.end()的调用吗?也就是说,即使在事件循环之外调用,proxyResponse仍会完成?
-
GET不会触发END事件是在捕获连接堆栈中某处的END?
- Dispatch incoming request to a static file (if it exists)
- Otherwise, dispatch the request to another service
Is this call to proxyRequest.end() safe? That is, will the proxyResponse still be completed even if this is called outside of the event loops?
Is it normal for GET to not trigger END events, or is the END being captured somewhere in the connect stack?
我有基本的工作,但现在试图让整个事情与Sencha 连接,所以我可以访问所有踢屁股中间件提供。
所有操作都发生在 下面
connect(
connect.logger(),
connect.static(__ dirname +'/ public'),
(request,response) - >
dispatchProxy请求,响应)
).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')
)
#这是从不触发GETs
request.on('end', - >
proxyRequest.end()
)
#so我必须有这里
proxyRequest.end()
我发现在处理GET请求时,END事件不会被触发,因此需要调用proxyRequest.end()。 POST请求同时触发DATA和END事件。
有几个问题:
少于 end
事件和更多数据
事件。如果客户端发出GET请求,则有标头,没有数据。这不同于POST,请求者正在发送数据,因此 on(data)
处理程序被命中。所以(原谅我的JS示例,我不熟悉coffeescript):
var http = require ');
//你不会看到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 \\\
');
})listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
如果我对这个服务器进行curl调用,数据事件不会被命中,因为 GET
请求只是标题。因此,你的逻辑变成:
//好的设置请求...
//但是,回调不会命中,直到你
//开始写一些数据或结束proxyRequest!
proxyRequest = http.request(options,(proxyResponse) - >
//所以这没有命中...
proxyResponse.on('data',(chunk) - >
response.write(chunk,'binary')
)
//这没有命中
proxyResponse.on('end ',(chunk) - >
//这就是为什么你的response.on(end)事件没有得到命中
response.end()
)
response.writeHead proxyResponse.statusCode,proxyResponse.headers
)
//这没有命中!
request.on('data',(chunk) - >
proxyRequest.write(chunk,'binary')
)
//直到你的proxyRequest
//回调处理程序被命中,这没有发生,因为
//不像POST你的GET请求没有数据
request.on('end ', - >
proxyRequest.end()
)
//现在代理请求调用最终完成了,其中
//触发了回调函数您的http请求设置
proxyRequest.end()
手动调用 proxyRequest.end()
,用于 GET
由于我刚刚提到的逻辑分支。
I am working on a cunning plan that involves using node.js as a proxy server in front of another service.
In short:
I have the basics working, but now attempting to get the whole thing working with Sencha Connect so I can access all the kick-ass middleware provided.
All of the action happens in dispatchProxy below
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()
You will notice proxyRequest.end() on the final line above.
What I have found is that when handling GET requests, the END event of the request is never triggered and therefore a call to proxyRequest.end() is required. POST requests trigger both DATA and END events as expected.
So several questions:
The problem is less the end
event and more the data
event. If a client makes a GET requests, there's headers and no data. This is different from POST, where the requester is sending data, so the on("data")
handler gets hit. So (forgive me for the JS example, I'm not that familiar with 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/');
If I make a curl call to this server, the data event never gets hit, because the GET
request is nothing more than headers. Because of this, your logic becomes:
// 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()
So yes you're going to have to manually call proxyRequest.end()
for GET
requests due to the logic branching I just mentioned.
这篇关于node.js http.request事件流 - 我的END事件在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!