node-http-proxy
node-http-proxy copied to clipboard
Create proxy server behind corporate proxy
i have anodejs application that uses http-proxy to create a proxy to send the incoming requests for example: https://localhost/api/login to https://server1/api/login. here is the code used :
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();
self.routes['/api/*'] = function(req, res) { proxy.proxyRequest(req, res, { target: "https://server1", changeOrigin: true }); }; this is working just fine in my machine. Now when i deploy this on a server, i get error { [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' } the problem is that there is another proxy let say (corporate proxy let call it: localProxy) between myserver and server1. I don't know where to set the localProxy in my code above. and where to set the server1 url? and is there is a way to use http-proxy in this case
If you are using HTTPS, wich it seems so, you should use https-proxy-agent. You can provide a proxy server to https-proxy-agent:
var HttpsProxyAgent = require('https-proxy-agent'); var proxyServer = process.env.http_proxy;
var proxy = httpProxy.createProxyServer({ agent: new HttpsProxyAgent(proxyServer) });
Great suggestion, it works.
I agree, this should be the go-to solution when one wants to use node-http-proxy to send requests through another proxy.
If you are using HTTPS, wich it seems so, you should use https-proxy-agent. You can provide a proxy server to https-proxy-agent:
var HttpsProxyAgent = require('https-proxy-agent'); var proxyServer = process.env.http_proxy;
var proxy = httpProxy.createProxyServer({ agent: new HttpsProxyAgent(proxyServer) });
this solution saves my day 8 years after 😆
Thansk @alfonso-presa !
By any chance this knowledge can be added to the documentation? The description for target, agent and forward are very vague. Unless you are very familiar with the code, it is unlikely you can connect these options together and understand when to use which.