I recently created a node.js app that reaches out to social media sites and caches our public feeds. I'm using some existing npm modules to facilitate accessing the social media api's. It works like a charm in my dev environment but on our production environment requests are timing out because they need to go through a proxy.
Without having to modify the npm modules how can I make the outbound requests go through a proxy?
Use the http.globalAgent property. This will let you intercept all requests running in your process. You can then modify those requests to be properly formatted for the proxy server.
http://nodejs.org/api/http.html#http_http_globalagent
Another option is to create a proxy exception for that application.
There is an npm module for that:
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 only want to proxy certain 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
});
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.