简体   繁体   中英

Can't attach an event for requests to Node's http-proxy

I'm trying to set up a proxy with logging in Node.js. I have tried doing it like https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events

The proxy works, but it seems the end event is not called - nothing is output to the console when I access the page. What am I missing?

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

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

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

});

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

server.listen(8000);

You aren't missing anything - it's a bug. The fix is already on master branch. It hasn't been deployed to npm yet, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM