HTTP Proxy using Connect and node-http-proxy

2019-02-16 02:38发布

问题:

I need a HTTP-Proxy to access an external API while I'm developing to get around cross domain security restrictions.

I found some example code here: http://nthloop.com/blog/local-dev-with-nodejs-proxy/

Which looks like exactly what I'm looking for, unfortunately when I've tried using it I get timed out trying to hit my local files.

No error in the console either.

Specifically the issue is from this section of code:

.use(function(req, res) {
  if (req.url.indexOf(endpoint.prefix) != -1) {
    proxy.proxyRequest(req, res, endpoint);
  }
})

My local server seems to get stuck in a loop here with no response, there are no errors in my console.

Charles http sniffer shows a request is made, but no response is received.

Any idea how I can get this working?

回答1:

Local server is getting stuck because the middleware does not delegate handling of cases when the if condition fails further down the middleware chain :

The following should solve the problem :


  .use(function(req, res, next) {
    if (req.url.indexOf(endpoint.prefix) === 0) {
      proxy.proxyRequest(req, res, endpoint);
    } else next();
  })


标签: node.js proxy