-
Notifications
You must be signed in to change notification settings - Fork 2k
proxy does not work within a callback function #168
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
Comments
As a sidenote: tried it the same way as the example "Proxy requests within another http server" as well - same behaviour |
It does work, when you use http.request() directly and i replaced node-http-proxy with an own small proxy module for now. |
did anybody at least verified this already? |
Thanks to AvianFlu i got the right way. The clue is, that the request object is a stream and you need to handle it right away or buffer it. In this case the following code works: var http = require('http'),
httpProxy = require('http-proxy'),
fs = require('fs');
//
// Create a proxy server with custom application logic
//
httpProxy.createServer(function (req, res, proxy) {
//
// Put your custom server logic here
//
console.log("##### PROXY: NEW REQUEST #####");
//
// Buffer the request so that `data` and `end` events
// are not lost during async operation(s).
//
var buffer = httpProxy.buffer(req);
/**
* In a callback function the target server never receives a request and client keeps on "pending"
*/
fs.readdir(__dirname, function(err, files)
{
console.dir(proxy);
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 9000,
buffer: buffer
});
});
}).listen(8000);
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
res.end();
console.log("##### TARGET: REQUEST RECEIVED #####");
}).listen(9000);
console.log("---\nProxy listens on port 8000 and routes anything to port 9000"); Take a look at lines 18 and 29. You need to create the buffer, hand it over to the proxy and then start to route the request. |
I am trying to call "proxy.proxyRequest()" within a callback function after reading some data, but this does not work at all (or at least i can't see my mistake :) )
Try to use my code below - with the request url "http://localhost:8000/" everything is fine and the request will be routed to the target server (same script - port 9000).
But when you try to load something with the url "http://localhost:8000/no-routing" first (or save or anything that needs a callback function), the target server never receives a request, no error will be raised and the client never receives a response and stays pending.
Nevertheless the function "proxyRequest()" and the node.js function "http.request()" will be called, but nothing goes to the target server.
Hope it stays an easy issue - any help is greatly appreciated :)
Cheers!
The text was updated successfully, but these errors were encountered: