How to override node.js http to use proxy for all outgoing requests

I recently created a node.js application that accesses social networking sites and caches our public channels. I use some existing npm modules to facilitate access to api on social networks. It works like a charm in my dev environment, but in our production environment requests fail because they need to go through a proxy.

Without the need to modify npm modules, how can I get outgoing requests to go through a proxy?

+6
source share
2 answers

Use the http.globalAgent property. This will allow you to intercept all requests running in your process. You can then modify these requests for proper formatting for the proxy server.

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

Another option is to create a proxy exception for this application.

+3
source

There is an npm module for this:

https://www.npmjs.com/package/global-tunnel

var globalTunnel = require('global-tunnel'); globalTunnel.initialize({ host: '10.0.0.10', port: 8080, sockets: 50 // optional pool size for each http and https }); 

Or, if you want to proxy specific requests, you can use the tunnel package (which is the driving force behind the global tunnel above):

https://www.npmjs.com/package/tunnel

 var tunnel = require('tunnel'); // create the agent var tunnelingAgent = tunnel.httpsOverHttps({ proxy: { host: 'localhost', port: 3128 } }); var req = https.request({ host: 'example.com', port: 443, // pass the agent in your request options agent: tunnelingAgent }); 
+2
source

Source: https://habr.com/ru/post/944530/


All Articles