在node.js中管道问题 [英] problems piping in node.js
问题描述
我们在node.js中有以下示例
We have the following example in node.js
var http = require('http');
http.createServer(function(request, response) {
var proxy = http.createClient(8083, '127.0.0.1')
var proxy_request = proxy.request(request.method, request.url, request.headers);
proxy_request.on('response', function (proxy_response) {
proxy_response.pipe(response);
response.writeHead(proxy_response.statusCode, proxy_response.headers);
});
setTimeout(function(){
request.pipe(proxy_request);
},3000);
}).listen(8081, '127.0.0.1');
该示例侦听127.0.0.1:8081中的请求,然后将其发送到127.0.0.1:8083中的虚拟服务器(始终返回200 OK状态代码).
The example listen to a request in 127.0.0.1:8081 and sends it to a dummy server (always return 200 OK status code) in 127.0.0.1:8083.
问题出在我们之前有一个异步模块(在这种情况下为setTimeOut计时)时,输入流(可读)和输出流(可写)之间存在管道.管道不起作用,并且没有任何内容通过8083端口发送到虚拟服务器.
The problem is in the pipe among the input stream (readable) and output stream (writable) when we have a async module before (in this case the setTimeOut timing). The pipe doesn't work and nothing is sent to dummy server in 8083 port.
也许,当在管道调用之前进行异步调用(在本例中为setTimeOut)时,inputstream更改为不可读"状态,并且在异步调用之后,管道不发送任何内容.
Maybe, when we have a async call (in this case the setTimeOut) before the pipe call, the inputstream change to a state "not readable", and after the async call the pipe doesn't send anything.
这只是一个示例...我们使用来自node.js社区的更多异步模块以相同的结果(ldapjs等)对其进行测试...
This is just an example...we test it with more async modules from node.js community with the same result (ldapjs, etc)...
我们尝试使用以下方法修复它: -request.read = true; //管道调用之前 -request.pipe(proxy_request,{end:false}); 结果相同(管道不起作用).
We try to fix it with: - request.readable =true; //before pipe call - request.pipe(proxy_request, {end : false}); with the same result (the pipe doesn't work).
有人可以帮助我们吗?
推荐答案
我认为从0.8.0开始有效.我没有尝试0.6.11 +
I believe this works as of 0.8.0. I didn't try in 0.6.11+
这篇关于在node.js中管道问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!