Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

A request within a request callback does not pipe back to server response #410

Closed
srcnix opened this Issue · 6 comments

2 participants

@srcnix

Any thoughts as to why having a request within a request callback will not pipe the request response back to the server.

// Load modules
var http        = require('http');
var https       = require('https');
var httpProxy   = require('http-proxy'); 
var url         = require('url');
var qstr        = require('querystring');
var path        = require('path');
var requester   = require('request');


var server = httpProxy.createServer(function(request, response) { 
  
  requester({url: 'http://www.facebook.com'}, function(error, body, resp) {
    console.log(body);
    proxy = requester({
      url: 'http://www.srcnix.com',
      method: 'GET'
    });      
    
    request.pipe(proxy);
    proxy.pipe(response);      
  });
  
});
@srcnix

The following code only works outside the parent request.

proxy = requester({
  url: 'http://www.srcnix.com',
  method: 'GET'
});      
    
request.pipe(proxy);
proxy.pipe(response);
@mikeal
Owner

take the pipe out of the callback and use the returned stream from request, not the callback response.

@srcnix

Sorry, I don't follow. Such as the following?

var server = httpProxy.createServer(function(request, response) { 
  
  requester({url: 'http://www.facebook.com'}, function(error, body, resp) {
    console.log(body);
    proxy = requester({
      url: 'http://www.srcnix.com',
      method: 'GET'
    });    
  });

  request.pipe(proxy);
  proxy.pipe(response);  
  
});

If so (And bare with me, as I'm new to nodejs), sure that wouldn't work due to the asynchronous manner of node? Not to mention proxy won't be available as out of scope...

@mikeal
Owner
var server = httpProxy.createServer(function(request, response) { 

  requester({url: 'http://www.facebook.com'}, function(error, body, resp) {
    console.log(body);   
  });

   var proxy = requester({
      url: 'http://www.srcnix.com',
      method: 'GET'
    }); 

  request.pipe(proxy);
  proxy.pipe(response);  

});
@srcnix

Thanks for the update. The issue that is arising is the need to perform the first request before the latter, as data from the first request is required in the latter.

@mikeal
Owner

ok, here's the issue, you're trying to pipe the incoming request to the last one after it's already ended. since aren't passing the request body you can fix this pretty simply.

var server = httpProxy.createServer(function(request, response) { 

  requester({url: 'http://www.facebook.com'}, function(error, body, resp) {
    console.log(body);
    proxy = requester({
      url: 'http://www.srcnix.com',
      method: 'GET'
    });    

    request.pipe(proxy);
    proxy.pipe(response);  

    process.nextTick(function () { proxy.end() })
  });
});
@mikeal mikeal closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.