在 Node.js 中建立代理
您可以使用 new Agent() 方法在 Node.js 中建立代理實例。 http.request() 方法使用「http」模組中的 globalAgent 建立自訂 http.Agent 實例。
語法
new Agent({options})
參數
上述函數可以接受以下參數−
選項< /strong> – 這些選項將包含建立時可以在代理程式上設定的可設定選項。以下是代理可以擁有的字段/選項-
keepAlive - 此方法保持套接字是否有任何未完成的請求或不,但保留它們以供將來的任何請求使用,而無需實際重新建立TCP 連線。可以使用「關閉連線」來關閉此連線。預設值: false。
keepAliveMsecs - 將 keepAlive 選項設為 true 時,此選項會定義 TCP keep-Alive 封包的初始延遲。預設值為 1000。
maxSockets - 此選項定義每個主機允許的最大套接字數。預設情況下,該值為無窮大。
maxTotalSockets – 所有主機允許的套接字總數。每個請求都使用一個新的套接字,直到達到限制。預設值為無窮大。
maxFreeSockets - 這是在空閒狀態下可以保持開啟狀態的空閒套接字的最大數量。預設值為:256。
調度 - 這是在選擇下一個要使用的空閒套接字時可以套用的調度策略。調度可以是“fifo”或“lifo”。
逾時 - 表示套接字逾時(以毫秒為單位)。
範例
建立一個名為agent.js的檔案並複製以下程式碼片段。建立檔案後,使用以下指令執行此程式碼,如下例所示-
node agent.js
agent.js
現場示範
// Node.js program to demonstrate the creation of new Agent // Importing the http module const http = require('http'); // Creating a new agent var agent = new http.Agent({}); // Defining options for agent const aliveAgent = new http.Agent({ keepAlive: true, maxSockets: 5, }); // Creating connection with alive agent var aliveConnection = aliveAgent.createConnection; // Creating new connection var connection = agent.createConnection; // Printing the connection console.log('Succesfully created connection with agent: ', connection.toString); console.log('Succesfully created connection with alive agent: ', aliveConnection.toString);
輸出
C:\homeode>> node agent.js Succesfully created connection with agent: function toString() { [native code] } Succesfully created connection with alive agent: function toString() { [native code] }
範例
「agentkeepalive」模組在嘗試建立套接字或代理程式時提供了更好的靈活性。我們將在下面的範例中使用此模組以便更好地理解。
安裝
npm install agentkeepalive --save
程式碼
#// Node.js program to demonstrate the creation of new Agent // Importing the http module const http = require('http'); // Importing the agentkeepalive module const Agent = require('agentkeepalive'); // Creating a new agent const keepAliveAgent = new Agent({}); // Implementing some options const options = { host: 'tutorialspoint.com', port: 80, path: '/', method: 'GET', agent: keepAliveAgent, }; // Requesting details via http server module const req = http.request(options, (res) => { // Printing statuscode, headers and other details // received from the request console.log("StatusCode: ", res.statusCode); console.log("Headers: ", res.headers); }); // Printing the agent options console.log("Agent Options: ", req.agent.options); req.end();
輸出
C:\homeode>> node agent.js Agent Options: { socketActiveTTL: 0, timeout: 30000, freeSocketTimeout: 15000, keepAlive: true, path: null } StatusCode: 403 Headers: { date: 'Sun, 25 Apr 2021 08:21:14 GMT', server: 'Apache', 'x-frame-options': 'SAMEORIGIN', 'last-modified': 'Thu, 16 Oct 2014 13:20:58 GMT', etag: '"1321-5058a1e728280"', 'accept-ranges': 'bytes', 'content-length': '4897', 'x-xss-protection': '1; mode=block', vary: 'User-Agent', 'keep-alive': 'timeout=5, max=100', connection: 'Keep-Alive', 'content-type': 'text/html; charset=UTF-8' }
以上是在 Node.js 中建立代理的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

如何在JavaScript中將具有相同ID的數組元素合併到一個對像中?在處理數據時,我們常常會遇到需要將具有相同ID�...

學習JavaScript不難,但有挑戰。 1)理解基礎概念如變量、數據類型、函數等。 2)掌握異步編程,通過事件循環實現。 3)使用DOM操作和Promise處理異步請求。 4)避免常見錯誤,使用調試技巧。 5)優化性能,遵循最佳實踐。

實現視差滾動和元素動畫效果的探討本文將探討如何實現類似資生堂官網(https://www.shiseido.co.jp/sb/wonderland/)中�...

JavaScript的最新趨勢包括TypeScript的崛起、現代框架和庫的流行以及WebAssembly的應用。未來前景涵蓋更強大的類型系統、服務器端JavaScript的發展、人工智能和機器學習的擴展以及物聯網和邊緣計算的潛力。

深入探討console.log輸出差異的根源本文將分析一段代碼中console.log函數輸出結果的差異,並解釋其背後的原因。 �...
