Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
srcnix opened this issue Jan 10, 2013 · 6 comments
Closed

Comments

@srcnix
Copy link

srcnix commented Jan 10, 2013

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
Copy link
Author

srcnix commented Jan 10, 2013

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
Copy link
Member

mikeal commented Jan 10, 2013

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

@srcnix
Copy link
Author

srcnix commented Jan 10, 2013

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
Copy link
Member

mikeal commented Jan 10, 2013

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
Copy link
Author

srcnix commented Jan 10, 2013

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
Copy link
Member

mikeal commented Jan 10, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants