2

I am using the http-proxy-middleware module, which is an express middleware. the middleware module relies on http-proxy. The node host is running behind a proxy. I want to forward certain routes to a different service (for test purposes let's assume httpbin.org). So I defined the proxy as follows.

var proxy = require('http-proxy-middleware');
var aeProxy = proxy({
    target: 'http://httpbin.org',
    changeOrigin: true,
    pathRewrite: {
        '^/api/ae':'/get'
    }
});
app.use('/api/ae', proxy);

I have also set the respective env variables (from debugging console):

process.env.HTTP_PROXY
> "http://proxy:8080"
process.env.HTTPS_PROXY
> "http://proxy:8080"

Unfortunately I only get timeouts. When running the node script in an environment without a proxy it works as expected.

Is my configuration wrong?

3
  • You have to set the ENV variables http_proxy = 'proxy.example.com:3129'; Commented Jan 24, 2017 at 16:19
  • sorry, forgot toadd that I have set those env variables. Making a request (using request module) to the same URL even works correctly. Commented Jan 24, 2017 at 16:21
  • Maybe this helps: github.com/chimurai/http-proxy-middleware/issues/22 Commented Jan 24, 2017 at 19:06

2 Answers 2

5

Credit to chimurai for this on how to connect via a corporate proxy via the agent field.

var HttpsProxyAgent = require('https-proxy-agent');
var proxy = require("http-proxy-middleware");

// corporate proxy to connect to via environment variables
var proxyServer = process.env.HTTPS_PROXY ||
                  process.env.HTTP_PROXY;

var options = {
    target: 'http://localhost:3000',//Proxy url
    agent: new HttpsProxyAgent(proxyServer)//The actual corporate proxy sever 
};

var apiProxy = proxy('/api', options);
Sign up to request clarification or add additional context in comments.

Comments

0

If you are behind a V2Ray protocol, you can just set the listening address and port of your connection like bellow and you'r good to go.

var HttpsProxyAgent = require('https-proxy-agent');
const { createProxyMiddleware, fixRequestBody } = require('http-proxy-middleware');

module.exports = function(app) {
  app.use(
    '/api',
    createProxyMiddleware({
      target: process.env.REACT_APP_API_URL
      changeOrigin: true,
      secure: false,
      logLevel: "debug",
      onProxyReq: fixRequestBody,
      agent: new HttpsProxyAgent('http://127.0.0.1:1087'),
      headers: {
        'X-Auth-Token': process.env.REACT_APP_API_TOKEN
      },
      pathRewrite: {
        '^/api': ''
      }
    })
  );
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.