Skip to content

res.on('end', callback) not working #214

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

Closed
subnetmarco opened this issue Mar 26, 2012 · 12 comments · Fixed by #216
Closed

res.on('end', callback) not working #214

subnetmarco opened this issue Mar 26, 2012 · 12 comments · Fixed by #216

Comments

@subnetmarco
Copy link
Contributor

My code:

var httpProxy = require('http-proxy');

httpProxy.createServer(function (req, res, proxy) {

    var buffer = httpProxy.buffer(req);

    res.on('end', function() {
        console.log("The request was proxied");
    });

    proxy.proxyRequest(req, res, {
      host: '127.0.0.1',
      port: 80, 
      buffer: buffer
    });

}).listen(8080);
@coderarity
Copy link
Contributor

You should listen to request's end event:

var httpProxy = require('http-proxy');

httpProxy.createServer(function (req, res, proxy) {

    var buffer = httpProxy.buffer(req);

    req.on('end', function() {
        console.log("The request was proxied");
    });

    proxy.proxyRequest(req, res, {
      host: '127.0.0.1',
      port: 80, 
      buffer: buffer
    });

}).listen(8080);

@subnetmarco
Copy link
Contributor Author

But if the req ends it doesn't imply the that res is closed as well, and that the response has been proxied successfully and completely.

@coderarity
Copy link
Contributor

Yeah, I see what you mean. The request would have been proxied, but the response back might not necessarily be done.

res.end seems to have been called just fine. Also, it seems that the proxy emits an end event when this happens. I'm not seeing either. Let me see if I can't figure out more.

@coderarity
Copy link
Contributor

http://nodejs.org/api/http.html#http_class_http_serverresponse

Apparently res is not supposed to emit an end event!

However - the proxy should emit an end event. I'm still not sure why this isn't happening.
EDIT: It's a RoutingProxy, it's the HttpProxy that emits the event. Let me see if I can't find the right way to get the HttpProxy object so you can listen for the end event.

@subnetmarco
Copy link
Contributor Author

I've already pointed out this issue in #157

@coderarity
Copy link
Contributor

Cool, thanks for linking that! I'll try to fix this right now.

@coderarity
Copy link
Contributor

Hey, I made a pull request for this here: #216

You'll probably want to use the following code to do this, otherwise you'll run into the case I mentioned in that pull request:

var http = require('http'),
    httpProxy = require('http-proxy');

var proxy = new httpProxy.RoutingProxy();

proxy.on('end', function() {
  console.log('proxied');
})

http.createServer(function (req, res) {
  proxy.proxyRequest(req, res, {
    host: 'localhost',
    port: 8080
  });
}).listen(8000);

Note that this will only work after the pull request is merged!

@coderarity
Copy link
Contributor

Check the pull request for an alternative way to do it. It might work better for you. Documentation was added in the pull request as well for this case.

@subnetmarco
Copy link
Contributor Author

Thanks, anyways I think this should be pushed in master.

@coderarity
Copy link
Contributor

@thefosk You should say that in the pull request =D

@subnetmarco
Copy link
Contributor Author

I think that the following code fixes it in the current non-patched version. Basically I've monkey patched the res.end() method:

var httpProxy = require('http-proxy');

httpProxy.createServer(function (req, res, proxy) {
    var buffer = httpProxy.buffer(req);

    middleware(res, function() {
        console.log("The request was proxied");
    });

    proxy.proxyRequest(req, res, {
      host: 'google.com',
      port: 80, 
      buffer: buffer
    });

}).listen(9091);


function middleware(res, callback) {
    var _end = res.end;
    res.end = function(data) {
        _end.call(res, data);
        if (callback) callback();
    }
}

This should be reliable as well. Thoughts?

@coderarity
Copy link
Contributor

Yeah, sure, if you're fine with your code like that. I mean, you're patching the res.end function every single request, but since a new res object is made every request, I guess that works. (although it's a little excessive to have to add the function every request) However, once the pull request is merged in and published, I would use the supported method with server.proxy anyways.

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

Successfully merging a pull request may close this issue.

2 participants