[英]How to override node.js http to use a proxy for all outbound requests
我最近創建了一個node.js應用程序,它可以訪問社交媒體網站並緩存我們的公共供稿。 我正在使用一些現有的npm模塊來方便訪問社交媒體API。 它在我的開發環境中就像一個魅力,但在我們的生產環境中,請求是超時的,因為它們需要通過代理。
無需修改npm模塊,如何使出站請求通過代理?
使用http.globalAgent屬性。 這將允許您攔截流程中運行的所有請求。 然后,您可以修改這些請求,以便為代理服務器正確格式化。
http://nodejs.org/api/http.html#http_http_globalagent
另一種選擇是為該應用程序創建代理例外。
有一個npm模塊:
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
});
或者,如果您只想代理某些請求,則可以使用隧道包(這是上面全局隧道的驅動力):
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
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.