node.js pipe restify http client response

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...

Answers


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);
    });
  });
});

Need Your Help

Spring-Boot multiple db target dependencies

java spring jdbc spring-boot

Is there a 'community/Spring' approved method with Boot to have different datasource target for the same project?

Is it possible to simulate keyboard/mouse event in NodeJS?

javascript node.js console

Imagine that a NodeJS module, when invoked from console, outputs some introductory messages and then waits for user input (click enter or esc). This module already has and does everything we require,