سؤال

I have this exact question, except I'm using the restify HttpClient:

node.js write http response to stream

For clarity, I'm trying to do this:

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

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

It hangs for a few seconds but never writes anything to stdout. Obviously I'm trying to fetch something other than google's homepage, but for example...

هل كانت مفيدة؟

المحلول

I would recommend you to user request for something like this:

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

Looking at restify's docs it seems you need to wait for the 'result' event:

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);
    });
  });
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top