【问题标题】:node.js pipe restify http client responsenode.js 管道恢复 http 客户端响应
【发布时间】:2014-04-14 08:28:30
【问题描述】:

我有这个确切的问题,除了我使用的是 restify HttpClient:

node.js write http response to stream

为了清楚起见,我正在尝试这样做:

var client = restify.createClient({
    url: "http://www.google.com"
});

client.get("/", function(err,res) {
    res.pipe(process.stdout);
});

它会挂起几秒钟,但从不向标准输出写入任何内容。显然,我正在尝试获取谷歌主页以外的其他内容,但例如......

【问题讨论】:

    标签: javascript node.js restify


    【解决方案1】:

    我会向用户request 推荐这样的事情:

    var request = require('request'); // npm install request
    request('http://www.google.com').pipe(process.stdout);
    

    看着restify's docs,看来你需要等待“结果”事件:

    var client = restify.createClient({
        url: "http://www.google.com"
    });
    
    client.get("/", function(err,req) {
      req.on('result',function(err2,res) {
        res.pipe(process.stdout);
        res.on('end',function() {
          process.exit(0);
        });
      });
    });
    

    【讨论】:

    • 你能在解释“结果”事件的文档中引用吗?我没看到。不过谢谢,这行得通。
    猜你喜欢
    • 2011-06-13
    • 1970-01-01
    • 2015-09-24
    • 2016-04-10
    • 2011-10-06
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多