2012-07-11 25 views
2

我使用node-curl作为HTTPS客户端向Web上的资源发出请求,代码在面向Internet的代理服务器后面运行。node-curl中的数据块(node.js)

的代码我使用合作:

var curl = require('node-curl'); 
//Call the curl function. Make a curl call to the url in the first argument. 
//Make a mental note that the callback to be invoked when the call is complete 
//the 2nd argument. Then go ahead. 
curl('https://encrypted.google.com/', {}, function(err) { 
    //I have no idea about the difference between console.info and console.log. 
    console.info(this.body); 
}); 
//This will get printed immediately. 
console.log('Got here'); 

节点卷曲检测从环境中的代理服务器设置,并还给了预期的效果。

面临的挑战是:在整个https响应被下载后,回调被触发,并且据我所知,http(s)模块中没有'data' and 'end' events的类似情况。另外,在通过源代码之后,我发现节点卷发库确实以块:参考线58的形式接收数据https://github.com/jiangmiao/node-curl/blob/master/lib/CurlBuilder.js。在这种情况下,目前似乎没有发生任何事件。

我需要将可能大小的响应转发回局域网中的另一台计算机进行处理,所以这是我的一个明确的问题。

正在使用node-curl为此目的在节点中推荐?

如果是,我该如何处理?

如果不是,那么将会是一个合适的替代方案?

回答

1

我会去美妙的request模块,至少如果代理设置不比它支持的更高级。单从环境中读取代理设置自己:

var request = require('request'), 
    proxy = request.defaults({proxy: process.env.HTTP_PROXY}); 

proxy.get('https://encrypted.google.com/').pipe(somewhere); 

或者,如果你不想pipe它:

var req = proxy.get({uri: 'https://encrypted.google.com/', encoding: 'utf8'}); 

req.on('data', console.log); 
req.on('end', function() { console.log('end') }); 

上面,我也通过encoding我预计的响应。您也可以在默认值中指定(上面调用request.defaults()),或者您可以将其保留,在这种情况下,您将在data事件处理程序中获得Buffer

如果你想要做的就是将它发送到另一个网址,要求是非常适合:

proxy.get('https://encrypted.google.com/').pipe(request.put(SOME_URL)); 

或者,如果你宁愿POST它:

proxy.get('https://encrypted.google.com/').pipe(request.post(SOME_URL)); 

或者,如果你想代理请求到目标服务器以及:

proxy.get('https://encrypted.google.com/').pipe(proxy.post(SOME_URL)); 
+1

我感谢你的及时和出色的单词编辑答案。我会回到你身上,看看我是否真的终于使用这个。但这是非常棒的。 – user1514989 2012-07-13 11:18:48

+0

谢谢你说 - 这实际上是什么让我和(我认为)所有的StackOverflow打勾!如果你不使用它,至少现在你知道请求模块,我发现它非常有用。 – 2012-07-13 11:29:19